SetTimer
Calls a function after a delay, once or repeatedly.
The function runs on the simulation thread in the tick the timer falls due - the same place a callback runs, so it may call
anything a callback may. A repeating timer runs every ms milliseconds until KillTimer; a one-shot timer
runs once and is gone. Timers belong to the mode: a reload discards them. The shortest interval is one tick (about 33 ms).
Syntax
Section titled “Syntax”SetTimer(fn, ms [, repeating])| Parameter | Type | |
|---|---|---|
fn |
function | the function to call (no arguments) |
ms |
number | the delay, and the interval of a repeating timer, in milliseconds |
repeating |
boolean | true repeats every ms; false or nothing fires once (optional) |
Returns
Section titled “Returns”number - the timer’s id, for KillTimer
Example
Section titled “Example”-- a countdown: "3", "2", "1", "Fight!"local left = 3local timertimer = SetTimer(function() if left > 0 then GameTextForAll(tostring(left), 900) left = left - 1 else KillTimer(timer) GameTextForAll("Fight!", 1500) endend, 1000, true)
-- once, in twenty secondsSetTimer(function() DestroyEntity(corpse) end, 20000)See also
Section titled “See also”KillTimer · OnTick · GetServerTime · the Server and timers group of the index
