Using Director > Parent Scripts > Creating timeout objects |  |

Creating timeout objects

You can create a timeout object—a script object that acts like a timer and sends a message when the timer expires. This is useful for scenarios that require specific things to happen at regular time intervals or after a particular amount of time has elapsed.

Timeout objects can send messages that call handlers inside child objects or in movie scripts. You create a timeout object by using the `new()` function. You must specify a name for the object, a handler to be called, and the frequency with which you want the handler to be called. Once a timeout object is created, Director keeps a list of currently active timeout objects, called `timeOutList`.

To create timeout objects, use the following syntax:

    variableName = timeOut("theName").new(integerMilliseconds, #handlerName, targetObject)

This statement uses the following elements:

| `variableName` is the variable you are placing the timeout object into.
| `timeOut` indicates which type of Lingo object you are creating.
| `theName` is the name you give to the timeout object. This name will appear in the `timeOutList`. It is the `#name` property of the object.
| `new` is the Lingo function that creates a new object.
| `integerMilliseconds` indicates the frequency with which the timeout object should call the handler you specify. This is the `#period` property of the object. A value of 2000 will call the specified handler every 2 seconds.
| `#handlerName` is the name of the handler you want the object to call. This is the `#timeOutHandler` property of the object. You represent it as a symbol by preceding the name with the `#` sign. For example, a handler called `on accelerate` would be specified as `#accelerate`.
| `targetObject` indicates which child object's handler should be called. This is the `#target` property of the object. It allows specificity when many child objects contain the same handlers. If you omit this parameter, Director will look for the specified handler in the movie script.

This statement creates a timeout object named `timer1` that will call the `on accelerate` handler in the child object `car1` every 2 seconds:

    myTimer = timeOut("timer1").new(2000, #accelerate, car1)

To determine when the next timeout message will be sent from a particular timeout object, check its `#time` property. The value returned is the point in time, in milliseconds, when the next timeout message will be sent.

This statement determines the time when the next timeout message will be sent from the timeout object `timer1` and displays it in the Message window:

    put timeout("timer1").time