Timers

setInterval()

Schedules a repeating callback. The delay is not an exact execution guarantee, and async callback work may overlap subsequent ticks. Clear the handle when the owner stops; a completion-driven loop is often simpler for asynchronous jobs. Tested on Node.js 22.16.0. Example output (one line per entry): ["3"].

Formal Syntax & Structure

nodejs syntaxnodejs
setInterval(callback[, delay][, ...args])

Parameters & Attributes

NameTypeDescription
callbackFunctionFunction invoked repeatedly.
delaynumberRequested delay in milliseconds.
argsanyAdditional callback arguments.

Return Value

Timeout

Handle used to manage the timer.

Working Example

Example Snippetnodejs
let ticks = 0;await new Promise(resolve => { const timer = setInterval(() => { if (++ticks === 3) {    clearInterval(timer);    resolve();} }, 1); });console.log(ticks);
Node.js Reference