Skip to content

AceTimer-3.0

embeddable This library can be embedded into your addon as a mixin; list it when you create the addon and its methods become available directly on your addon object (via self).
Creating an addon object →

AceTimer-3.0 provides a central facility for registering timers.

AceTimer supports one-shot timers and repeating timers. All timers are stored in an efficient data structure that allows easy dispatching and fast rescheduling. Timers can be registered or canceled at any time, even from within a running timer, without conflict or large overhead.

AceTimer is currently limited to firing timers at a frequency of 0.01s as this is what the WoW timer API restricts us to.

All :Schedule functions will return a handle to the current timer, which you will need to store if you need to cancel the timer you just registered.

API Reference

Embed

method#
lua
AceTimer:Embed(target)

Copies AceTimer's methods onto target so you can call them on it directly. See The :Embed method.

Parameters

ParameterTypeDefaultDescription
targettableTarget object to embed AceTimer in.

Returns

TypeDescription
tableThe target object that was embedded.

ScheduleTimer

method#
lua
AceTimer:ScheduleTimer(func, delay, ...)

Schedule a new one-shot timer.

The timer will fire once in delay seconds, unless canceled before.

Parameters

ParameterTypeDefaultDescription
funcfunction | methodnameCallback function for the timer pulse (funcref or method name).
delaynumberDelay for the timer, in seconds.
...anyAn optional, unlimited amount of arguments to pass to the callback function.

Returns

TypeDescription
stringThe timer handle, used to cancel the timer with :CancelTimer.

Example

lua
function MyAddon:OnEnable()
    self:ScheduleTimer("TimerFeedback", 5)
end

function MyAddon:TimerFeedback()
    print("5 seconds passed")
end

ScheduleRepeatingTimer

method#
lua
AceTimer:ScheduleRepeatingTimer(func, delay, ...)

Schedule a repeating timer.

The timer will fire every delay seconds, until canceled.

Parameters

ParameterTypeDefaultDescription
funcfunction | methodnameCallback function for the timer pulse (funcref or method name).
delaynumberDelay for the timer, in seconds.
...anyAn optional, unlimited amount of arguments to pass to the callback function.

Returns

TypeDescription
stringThe timer handle, used to cancel the timer with :CancelTimer.

Example

lua
function MyAddon:OnEnable()
    -- store the handle so the timer can be cancelled later
    self.pulseTimer = self:ScheduleRepeatingTimer("Pulse", 5)
end

function MyAddon:Pulse()
    -- runs every 5 seconds until cancelled
end

CancelTimer

method#
lua
AceTimer:CancelTimer(id)

Cancels a timer with the given id, registered by the same addon object as used for :ScheduleTimer.

Both one-shot and repeating timers can be canceled with this function, as long as the id is valid and the timer has not fired yet or was canceled before.

Parameters

ParameterTypeDefaultDescription
idstringThe id of the timer, as returned by :ScheduleTimer or :ScheduleRepeatingTimer.

Returns

TypeDescription
booleantrue if the timer was successfully cancelled, false if the id was invalid or the timer already fired/was cancelled.

Example

lua
function MyAddon:OnEnable()
    self.timer = self:ScheduleRepeatingTimer("Pulse", 1)
end

function MyAddon:StopPulse()
    self:CancelTimer(self.timer)
end

CancelAllTimers

method#
lua
AceTimer:CancelAllTimers()

Cancels all timers registered to the current addon object (self).

TimeLeft

method#
lua
AceTimer:TimeLeft(id)

Returns the time left for a timer with the given id, registered by the current addon object (self).

This function will return 0 when the id is invalid.

Parameters

ParameterTypeDefaultDescription
idstringThe id of the timer, as returned by :ScheduleTimer or :ScheduleRepeatingTimer.

Returns

TypeDescription
numberThe time left on the timer.

Ace3, a World of Warcraft addon framework.