wowpedia
Main Menu


Schedules a (repeating) timer that can be canceled.

cbObject = C_Timer.NewTimer (seconds, callback)
         = C_Timer.NewTicker(seconds, callback [, iterations])

Arguments

seconds
number - Time in seconds between each iteration.
callback
function - Callback function to run.
iterations
number? - Number of times to repeat, or indefinite if omitted.

Returns

cbObject
userdata - Timer handle with :Cancel() and :IsCancelled() methods.

Details

Example

Schedules a repeating timer which runs 4 times.

/run C_Timer.NewTicker(2.5, function() print(GetTime()) end, 4)

Schedules a timer but cancels it right away.

local myTimer = C_Timer.NewTimer(3, function() print("Hello") end)
print(myTimer:IsCancelled()) -- false
myTimer:Cancel()
print(myTimer:IsCancelled()) -- true

Schedules a timer that prints a value written to a field stored on the timer handle itself. Timer handles all reference the same shared state internally, so user-defined fields written to handles can be used to supply additional data for use by the callback.

local myTimer = C_Timer.NewTimer(2.5, function(self) print("self.data is:", self.data) end)
myTimer.data = GetTime()

Schedules a repeating timer which runs 4 times and prints the timer handle returned from the function and supplied to the callback. Note that each print will result in a different object address being displayed, indicating the timer handles have a distinct identity.

/run print(C_Timer.NewTicker(0.25, function(self) print(self) end, 4))

Patch changes

Dragonflight Patch 10.0.0 (2022-10-25): Implementation moved to native code. APIs no longer accept non-function callback arguments, and now return userdata handles.

See also