Timer Functions
Timing helpers similar to browsers and Node.js. These functions are registered on the global object and are available in both Main and UI scripts.
Note
timers is a global module, no import is needed, and it is available only in Main script. It is not available in UI script.
Table of Contents
setTimeout(callback, delay [, ...args])
Executes callback once after the specified delay.
Parameters
callback(function): Function to run after the delay.delay(number, optional): Milliseconds to wait before executing. Defaults to0. Negative values are clamped to0....args(any, optional): Additional arguments passed tocallbackwhen it fires.
Return Value
- Type:
number - Description: Timer ID that can be passed to
clearTimeout()to cancel.
Example
javascript
const id = setTimeout(() => {
console.log("Fired after 3 seconds");
}, 3000);
// With extra arguments
setTimeout((greeting, name) => {
console.log(greeting, name);
}, 1000, "Hello", "Novadesk");setInterval(callback, interval [, ...args])
Executes callback repeatedly at the given interval.
Parameters
callback(function): Function to run on each tick.interval(number, optional): Milliseconds between executions. Defaults to0. Negative values are clamped to0....args(any, optional): Additional arguments passed tocallbackon each tick.
Return Value
- Type:
number - Description: Timer ID that can be passed to
clearInterval()to cancel.
Example
javascript
let tick = 0;
const id = setInterval(() => {
tick += 1;
console.log("tick", tick);
if (tick >= 5) {
clearInterval(id);
console.log("Interval stopped");
}
}, 1000);clearTimeout(id)
Cancels a timeout created with setTimeout().
Parameters
id(number): Timer ID returned bysetTimeout().
Example
javascript
const id = setTimeout(() => {
console.log("This will not run");
}, 5000);
clearTimeout(id);clearInterval(id)
Cancels an interval created with setInterval().
Parameters
id(number): Timer ID returned bysetInterval().
Example
javascript
const id = setInterval(() => {
console.log("Repeating");
}, 1000);
setTimeout(() => {
clearInterval(id);
console.log("Interval stopped after 5 seconds");
}, 5000);