Skip to main content
Version: latest - a1.7.0 ⚖️

⏱️ Timer

Execute of code at specified time intervals

info

🟦🟧 Authority: This 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.

Static Functions

ReturnsNameDescription
numberSetTimeoutExecutes a function, after waiting a specified number of milliseconds
numberSetIntervalSame 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
IsValidChecks if a Timer is currently active or waiting to be triggered
numberGetElapsedTimeReturns the time elapsed since the last tick
numberGetRemainingTimeReturns the time remaining to the next tick
PausePauses the Timer
ResumeResumes the Timer

SetTimeout

Executes a function, after waiting a specified number of milliseconds

Returns the timeout_id

Timer.SetTimeout(function(param1, param2)
Package.Log("Timeout after 3 seconds! Param1: " .. param1 .. ". Param2: " .. param2)
end, 3000, "hello", 123)
TypeParameterDefault ValueDescription
functioncallbackThe callback that will be executed
numbermilliseconds0The time in milliseconds to wait before executing the function
anyparameters...Additional parameters to pass to the function

SetInterval

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

Returns the interval_id

Timer.SetInterval(function(param1, param2)
Package.Log("Triggered each 2 seconds! Param1: " .. param1 .. ". Param2: " .. param2)
end, 2000, "world", 456)
TypeParameterDefault ValueDescription
functioncallbackThe callback that will be executed
numbermilliseconds0The time in milliseconds the timer should delay in between executions of the specified function
anyparameters...Additional parameters to pass to the function

ClearTimeout

Stops the execution of the function specified in SetTimeout()

-- Creates the Timeout
local my_timeout = Timer.SetTimeout(function() end, 3000)

-- Stops the Timeout
Timer.ClearTimeout(my_timeout)
TypeParameterDefault ValueDescription
numberinterval_idThe ID value returned by SetTimeout() is used as the parameter for this method

ClearInterval

Stops the execution of the function specified in SetInterval()

-- Creates the Interval
local my_interval = Timer.SetInterval(function() end, 3000)

-- Stops the Interval
Timer.ClearInterval(my_interval)
TypeParameterDefault ValueDescription
numberinterval_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

local character = Character(...)

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
Timer.Bind(my_timer, 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
TypeParameterDefault ValueDescription
numbertimer_idThe Timer ID
ActoractorActor to be bound

IsValid

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

Timer.IsValid(timer_id)
TypeParameterDefault ValueDescription
numbertimer_idThe Timer ID

GetElapsedTime

Returns the time elapsed since the last tick

local elapsed_time = Timer.GetElapsedTime(timer_id)
TypeParameterDefault ValueDescription
numbertimer_idThe Timer ID

GetRemainingTime

Returns the time remaining to the next tick

local remaining_time = Timer.GetRemainingTime(timer_id)
TypeParameterDefault ValueDescription
numbertimer_idThe Timer ID

Pause

Pauses the Timer

Timer.Pause(timer_id)
TypeParameterDefault ValueDescription
numbertimer_idThe Timer ID

Resume

Resumes the Timer

Timer.Resume(timer_id)
TypeParameterDefault ValueDescription
numbertimer_idThe Timer ID

Examples

-- creates a Interval to call a function at every 1 second
local my_interval = Timer.SetInterval(1000, function()
Package.Log("Tick 1 second!")
end)

-- cancels the Interval
Timer.ClearInterval(my_id)

-- 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")