Skip to main content
Version: bleeding-edge 🩸

⏱️ Timer

Execute code at specified time intervals.

πŸ’‚Authority
This static class can be accessed on both 🟧 Client and 🟦 Server.
πŸ—ΏStatic Class
This is a Static Class. You can access it's methods directly with .. It is not possible to initialize or create new instances.
info

The shortest interval possible is equal to the local Tick Rate - usually at 33ms. On the Server this can vary depending on the Config.toml setting.

πŸŽ’ Examples​

-- creates a Interval to call a function at every 2 seconds
local my_interval = Timer.SetInterval(function(param1, param2)
Package.Log("Triggered each 2 seconds! Param1: " .. param1 .. ". Param2: " .. param2)
end, 2000, "awesome param 1", 456)

-- cancels the Interval
Timer.ClearInterval(my_interval)

-- creates a Timeout to call my_function in 5 seconds, once - passing a parameter
Timer.SetTimeout(function(my_param)
Package.Log("nanos " .. my_param)
end, 5000, "world")
local character = Character(...)

local my_timer = Timer.SetTimeout(function(_character)
-- Do something with _character
-- Ex: Destroy the character after 10 seconds
_character:Destroy()
end, 10000, character)

-- Binds the Timer to the Character
-- This will ensure it will never trigger if the character is destroyed before it
-- With this you don't need to validate if the '_character' parameter is valid
Timer.Bind(my_timer, character)

πŸ—Ώ Static Functions​

ReturnsNameDescription
number SetTimeoutExecutes a function, after waiting a specified number of milliseconds
number SetIntervalSame as SetTimeout(), but repeats the execution of the function continuously
ClearTimeoutStops the execution of the function specified in SetTimeout()
ClearIntervalStops the execution of the function specified in SetInterval()
BindBinds a Timer to any Actor. The timer will be automatically cleared when the Actor is destroyed
boolean IsValidChecks if a Timer is currently active or waiting to be triggered
number GetElapsedTimeReturns the time elapsed since the last tick
number GetRemainingTimeReturns the time remaining to the next tick
PausePauses the Timer
ResumeResumes the Timer
ResetElapsedTimeResets a Timer to start from beginning

SetTimeout​

Executes a function, after waiting a specified number of milliseconds

Returns number (the timeout_id)
local ret = Timer.SetTimeout(callback, milliseconds?, parameters...?)
TypeParameterDefaultDescription
function callbackThe callback that will be executed
number milliseconds?0The time in milliseconds to wait before executing the function
any parameters...?nilAdditional parameters to pass to the function

SetInterval​

Same as SetTimeout(), but repeats the execution of the function continuously

Returns number (the interval_id)
local ret = Timer.SetInterval(callback, milliseconds?, parameters...?)
TypeParameterDefaultDescription
function callbackThe callback that will be executed.
Return false to stop it from being called.
number milliseconds?0The time in milliseconds the timer should delay in between executions of the specified function
any parameters...?nilAdditional parameters to pass to the function

ClearTimeout​

Stops the execution of the function specified in SetTimeout()
Timer.ClearTimeout(timeout_id)
TypeParameterDefaultDescription
number timeout_idThe ID value returned by SetTimeout() is used as the parameter for this method

ClearInterval​

Stops the execution of the function specified in SetInterval()
Timer.ClearInterval(interval_id)
TypeParameterDefaultDescription
number interval_idThe ID value returned by SetInterval() is used as the parameter for this method

Bind​

Binds a Timer to any Actor. The timer will be automatically cleared when the Actor is destroyed
Timer.Bind(timer_id, actor)
TypeParameterDefaultDescription
number timer_idThe Timer ID
Base Actor actorActor to be bound

IsValid​

Checks if a Timer is currently active or waiting to be triggered

Returns boolean ()
local ret = Timer.IsValid(timer_id)
TypeParameterDefaultDescription
number timer_idThe Timer ID

GetElapsedTime​

Returns the time elapsed since the last tick

Returns number ()
local ret = Timer.GetElapsedTime(timer_id)
TypeParameterDefaultDescription
number timer_idThe Timer ID

GetRemainingTime​

Returns the time remaining to the next tick

Returns number ()
local ret = Timer.GetRemainingTime(timer_id)
TypeParameterDefaultDescription
number timer_idThe Timer ID

Pause​

Pauses the Timer
Timer.Pause(timer_id)
TypeParameterDefaultDescription
number timer_idThe Timer ID

Resume​

Resumes the Timer
Timer.Resume(timer_id)
TypeParameterDefaultDescription
number timer_idThe Timer ID

ResetElapsedTime​

Resets a Timer to start from beginning
Timer.ResetElapsedTime(timer_id)
TypeParameterDefaultDescription
number timer_idThe Timer ID

πŸš€ Events​

This entity doesn't have own events.