-
Notifications
You must be signed in to change notification settings - Fork 0
SFUtils_CallLater
SFUtils.CallLater is a lightweight timer utility that extends the native zo_callLater API with a higher-level object-oriented interface.
It supports:
- One-shot timers
- Periodic timers
- Automatic retry on callback failure
- Callback argument passing
- Safe callback execution through
LibSFUtils.safeCall - Runtime timer management (start, cancel, destroy)
A simple diagram to display normal callback behaviour.
New()
│
▼
Start()
│
▼
Running
│
├── callback succeeds
│ │
│ ▼
│ Complete
│
├── callback fails
│ │
│ ▼
│ Retry
│
└── Cancel()
│
▼
Destroyed
| Constructor | One-shot | Periodic | Retries | Arguments |
|---|---|---|---|---|
New() |
✓ | ✓ | ||
NewSingle() |
✓ | ✓ | ||
NewMaxTries() |
✓ | ✓ | ✓ | |
NewTimer() |
✓ |
| You want... | Use |
|---|---|
| Run once | New() |
| Run once with retries | NewMaxTries() |
| Repeat forever | NewTimer() |
| Alias for compatibility | NewSingle() |
| Method | Description |
|---|---|
SetCallback() |
Replaces the callback. |
SetDelay() |
Changes the default delay. |
| Method | Description |
|---|---|
Start() |
Starts the timer. |
StartWithArgs() |
Starts with callback arguments. |
Cancel() |
Stops and cleans up the timer. (Cannot be restarted!) |
Destroy() |
Alias for Cancel(), returns nil. |
SetCallback() |
Replaces the callback. |
SetDelay() |
Changes the default delay. |
| Method | Description |
|---|---|
IsRunning() |
Returns whether the timer is active. |
Creates a timer that executes once.
local timer = SF.CallLater:New(function()
d("Executed")
end, 1000)
timer:Start()CallLater:New(callback, delayMs)| Parameter | Type | Description |
|---|---|---|
| callback | function | Function to execute. |
| delayMs | number | Delay in milliseconds before execution. Default is 0. |
A new CallLater timer object.
NewSingle() is simply an alias for New().
local timer = SF.CallLater:NewSingle(callback, 500)Creates a one-shot timer that automatically retries if the callback throws an error.
local timer = SF.CallLater:NewMaxTries(function()
error("Failure")
end, 1000, 3)
timer:Start()CallLater:NewMaxTries(callback, delayMs, maxTries)| Parameter | Type | Description |
|---|---|---|
| callback | function | Function to execute. |
| delayMs | number | Delay between attempts. |
| maxTries | number | Maximum number of attempts to make. |
- Retries only occur when the callback throws an error.
- Successful execution clears retry tracking.
- maxTries count includes the initial execution attempt.
Creates a timer that repeats indefinitely until cancelled.
local timer = SF.CallLater:NewTimer(function()
d("Tick")
end, 1000)
timer:Start()CallLater:NewTimer(callback, intervalMs)| Parameter | Type | Description |
|---|---|---|
| callback | function | Function called every interval. |
| intervalMs | number | Interval in milliseconds. |
Starts a timer.
timer:Start()Optionally override the default delay.
timer:Start(500)timer:Start(delayMs)| Parameter | Type | Description |
|---|---|---|
| delayMs | number | Optional delay override for one-shot timers. |
For one-shot timers:
- Cancels any currently running instance.
- Starts a new delayed callback.
For periodic timers:
- Begins the recurring timer loop.
Returns the timer instance to allow chaining.
Starts a one-shot timer while supplying arguments to the callback.
local timer = SF.CallLater:New(function(name, score)
d(string.format("%s scored %d", name, score))
end, 1000)
timer:StartWithArgs("Lumo", 9000)timer:StartWithArgs(...)- Only available for one-shot timers.
- Arguments are stored until execution.
- Calling this on a periodic timer logs a warning.
Stops a running timer. After cancellation the timer object cannot simply be restarted; create a new timer or assign a new callback before reuse.
timer:Cancel()-
trueif the timer was cancelled. -
falseif it was not running.
Cancelling also clears:
- callback
- pending arguments
- retry information
- periodic callback
- interval
- timer handle
After cancellation the timer object cannot simply be restarted; create a new timer or assign a new callback before reuse.
Alias for Cancel().
timer = timer:Destroy()Returns nil, making cleanup convenient.
Returns whether the timer is currently active.
if timer:IsRunning() then
d("Still running")
endtrueor
falseReplaces the callback.
timer:SetCallback(function()
d("New callback")
end)timer:SetCallback(callback)Returns the timer instance.
Changes the default delay used by one-shot timers.
timer:SetDelay(2000)timer:SetDelay(delayMs)Changing the delay does not affect a currently running timer.
The new delay is used the next time Start() is called.
Returns the timer instance.
All callbacks execute through:
LibSFUtils.safeCall()This prevents Lua errors from propagating into the addon.
If the callback fails:
- retry counter increments
- timer is rescheduled if retries remain
- retry tracking is cleared once exhausted
If the callback fails:
- error is logged
- next interval continues normally
The periodic timer is not cancelled by callback errors.
Most methods return the timer instance.
local timer =
SF.CallLater:New(callback, 1000)
:SetDelay(500)
:Start()SF.CallLater:New(function()
d("Finished")
end, 2000):Start()SF.CallLater:New(function(player, gold)
d(player .. " has " .. gold)
end, 500):StartWithArgs("@Player", 25000)local timer = SF.CallLater:NewMaxTries(function()
assert(IsPlayerActivated())
end, 1000, 5)
timer:Start()local heartbeat = SF.CallLater:NewTimer(function()
d("Heartbeat")
end, 1000)
heartbeat:Start()Later:
heartbeat:Cancel()- One-shot timers use the native
zo_callLater. - Periodic timers are implemented by rescheduling themselves after each execution.
- All secure callback execution is protected by
LibSFUtils.safeCall. - Retry logic is available only for one-shot timers.
-
StartWithArgs()is supported only for one-shot timers. - Timer objects maintain their own execution state, making multiple concurrent timers independent of one another.