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.

 

Purpose

One set, a timer will keep calling the specified function 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). So, if you've added properties or methods to the function, they won't be available from within the function when it's run by the timer.

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.

Syntax

Code Block
themeRDark
languagejavascript
linenumberstrue
setTimer(functionName, delay[, gameObject]);

Parameters

ParameterMandatoryTypeDescription
functionName(tick)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 or anything like that.If you pass in a function, or the name of a function not on the global scope, the timer will break.

delay(tick)Number

The amount of time, in milliseconds, to wait before the function is called.

Avoid setting delays less than 110ms, because gameTime only gets incremented once per 100ms.

gameObject(error)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.

Return value

ValueTypeDescription
undefinedUndefinedThe timer was set.
<error>ErrorDid you forget to specify functionName as a string?

Example

Code Block
themeRDark
languagejavascript
titleCall function "foo" every second
linenumberstrue
function foo(gameObj) {
  // do stuff
}
// after 1000ms (1 second), global["foo"](someGameObject) will be called:
setTimer("foo", 1000, someGameObject);
Div
classnotice box ping

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
classbox
Availability
Status
colourGreen
title3.1 b1+

Requires:

  • Warzone 3.1 Beta 1 or above
Div
classbox
Contents

AssimilateJump to:

Table of Contents
excludemaxLevelContents5

Div
classbox

See also

Related functions:

  • removeTimer() – cancels all timers associated with a function
  • queue() – queue a function call to a later frame (internally this actually uses the setTimer() code!)