C_Timer.NewTimer

From Warcraft Wiki
Jump to navigation Jump to search
GitHub Octocat.png  Townlong-Yak BAD.png  ProfIcons engineering.png  Wowprogramming.png  BTNTemp.png C_Timer.After TheWarWithin-Icon-Inline.pngDragonflight-Icon-Inline.pngCata-Logo-Small.pngWrath-Logo-Small.pngWoW Icon update.png + 6.0.2 / 1.13.2
GitHub Octocat.png  Townlong-Yak BAD.png  ProfIcons engineering.png  BTNTemp.png C_Timer.NewTimer TheWarWithin-Icon-Inline.pngDragonflight-Icon-Inline.pngWrath-Logo-Small.pngWoW Icon update.png + 6.0.2 / 1.15.0
GitHub Octocat.png  Townlong-Yak BAD.png  ProfIcons engineering.png  BTNTemp.png C_Timer.NewTicker TheWarWithin-Icon-Inline.pngDragonflight-Icon-Inline.pngWrath-Logo-Small.pngWoW Icon update.png + 6.0.2 / 1.15.0

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|FunctionContainer - Callback function to run.
iterations
number? - Number of times to repeat, or indefinite if omitted.

Returns

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

Details

  • The callback function will be supplied a view of the timer handle (cbObject) when invoked.
    • The handle returned from the function and the one supplied to the callback are distinct objects that both reference a shared state. See the examples for more details.
  • C_Timer.NewTimer() has just a single iteration and is essentially the same as C_Timer.NewTicker(duration, callback, 1)
  • Errors thrown inside a callback function will not halt the ticker.

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