Ace3

API/AceTimer-3.0

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, rescheduled 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.1s. This constant may change in the future, but for now it seemed like a good compromise in efficiency and accuracy.

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

AceTimer-3.0 can be embeded into your addon, either explicitly by calling AceTimer:Embed(MyAddon) or by specifying it as an embeded library in your AceAddon. All functions will be available on your addon object and can be accessed directly, without having to explicitly call AceTimer itself.
It is recommended to embed AceTimer, otherwise you'll have to specify a custom `self` on all calls you make into AceTimer.


AceTimer:CancelAllTimers()

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


AceTimer:CancelTimer(handle, silent)

Cancels a timer with the given handle, 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 `handle` is valid and the timer has not fired yet or was canceled before.

Parameters

handle
The handle of the timer, as returned by `:ScheduleTimer` or `:ScheduleRepeatingTimer`
silent
If true, no error is raised if the timer handle is invalid (expired or already canceled)

Return value

True if the timer was successfully cancelled.


AceTimer:ScheduleRepeatingTimer(callback, delay, arg)

Schedule a repeating timer.
The timer will fire every `delay` seconds, until canceled.

Parameters

callback
Callback function for the timer pulse (funcref or method name).
delay
Delay for the timer, in seconds.
arg
An optional argument to be passed to the callback function.

Usage

MyAddon = LibStub("AceAddon-3.0"):NewAddon("TimerTest", "AceTimer-3.0")

function MyAddon:OnEnable()
  self.timerCount = 0
  self.testTimer = self:ScheduleRepeatingTimer("TimerFeedback", 5)
end

function MyAddon:TimerFeedback()
  self.timerCount = self.timerCount + 1
  print(("%d seconds passed"):format(5 * self.timerCount))
  -- run 30 seconds in total
  if self.timerCount == 6 then
    self:CancelTimer(self.testTimer)
  end
end


AceTimer:ScheduleTimer(callback, delay, arg)

Schedule a new one-shot timer.
The timer will fire once in `delay` seconds, unless canceled before.

Parameters

callback
Callback function for the timer pulse (funcref or method name).
delay
Delay for the timer, in seconds.
arg
An optional argument to be passed to the callback function.

Usage

MyAddon = LibStub("AceAddon-3.0"):NewAddon("TimerTest", "AceTimer-3.0")

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

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


AceTimer:TimeLeft(handle)

Returns the time left for a timer with the given handle, registered by the current addon object ('self').
This function will raise a warning when the handle is invalid, but not stop execution.

Parameters

handle
The handle of the timer, as returned by `:ScheduleTimer` or `:ScheduleRepeatingTimer`

Return value

The time left on the timer, or false if the handle is invalid.


You must login to post a comment. Don't have an account? Register to get one!