Timers

Managed Toolbox timers and cleanup when leaving the game.

Managed timers

Use mod methods instead of Node.js global timers: Toolbox tracks and clears them on unload. The network interface also clears timeouts and intervals on leave_game, including character logout and closing the game connection.

Method Purpose
mod.setTimeout(callback, delay, ...args) One call after a delay in milliseconds
mod.clearTimeout(handle) Cancel a timeout
mod.setInterval(callback, delay, ...args) Repeated calls
mod.clearInterval(handle) Cancel an interval
mod.clearAllTimeouts() Cancel all timeouts belonging to this interface
mod.clearAllIntervals() Cancel all intervals belonging to this interface
mod.setImmediate(callback, ...args) Deferred event-loop callback
mod.clearImmediate(handle) Cancel an immediate
mod.clearAllImmediates() Cancel all immediates

Use the matching cancellation method: do not pass an interval to clearTimeout. activeTimeouts and activeIntervals are Set objects for inspection; do not modify them manually.

Example

const timer = mod.setTimeout(() => {
    mod.log('One second later');
}, 1000);

// If the action becomes unnecessary:
mod.clearTimeout(timer);

A timer does not guarantee an exact execution time: the callback runs when the event loop can process it. This distribution converts delays to numbers and throws RangeError above 2147483647. Use finite nonnegative values. setImmediate is cleaned up on unload but is not part of the timeout/interval cleanup on leave_game.

Sources: doc/mod/timers.md, bin/mod.js.