Versions Compared
Key
- This line was added.
- This line was removed.
- Formatting was changed.
Excerpt |
---|
Allows you to trigger a function at some point in the future. |
Syntax
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
// syntax #1: optional game object as third parameter (WZ 3.1 b1+) setTimer(functionName, delay[, gameObject]); // syntax #2: optional string as third parameter (WZ 3.2+) setTimer(functionName, delay[, str]); |
Parameters
Syntax | Parameter | Mandatory | Type | Description | Game version |
---|---|---|---|---|---|
All | functionName | String | A string containing the name of the function to call. The function must be defined on the global scope - it cannot be a method of an object. | 3.1 Beta 1 | |
All | delay | Number | The amount of time, in milliseconds, to wait before the function is called. The minimum delay is 100 milliseconds. If you set a lower value, 100ms will be used. | 3.1 Beta 1 | |
#1 | gameObject | Game object | An optional game object that will be passed in to your function when it gets called. The object will automatically be refreshed with the latest game data. If the object gets destroyed, the associated timer will be cancelled automatically. | 3.1 Beta 1 | |
#2 | str | String | An optional string that will be passed in to your function when it gets called. | 3.2 |
Return value
Value | Type | Description | Game version |
---|---|---|---|
undefined | Undefined | The timer was set. | 3.1 Beta 1 |
<error> | Error | Did you forget to specify functionName as a string? | 3.1 Beta 1 |
Notes
One set, a timer will keep calling the specified function at regular intervals until you stop the timer using removeTimer().
The function, when called, will be run in the context of the global scope ("this" will be the global scope).
Timers persist the save/load cycle. You can therefore look at Saved game files to see what timers were running at the time when the game was saved.
Examples
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
function foo(gameObj) { // gameObj == someGameObject } // this syntax works on WZ 3.1 and above setTimer("foo", 100, someGameObject); // global function foo will be called every 100ms // if someGameObject gets destroyed, the timer will be cancelled |
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
function bar(str) { // str = "hello" } // this syntax requires WZ 3.2 or above queuesetTimer("bar", 1000, "hello"); // global function bar will be called after 1 second |
Div | ||
---|---|---|
| ||
A common mistake is to assume that the setTimer() is the same as Javascript's setTimeout() function. While it serves a similar purpose, it's definitely not the same as the Javascript implementation. |
Div | ||||||
---|---|---|---|---|---|---|
| ||||||
Availability
Requires:
|
Div | ||||
---|---|---|---|---|
| ||||
ContentsJump to:
|
Div | ||
---|---|---|
| ||
See alsoRelated functions:
|