On this page

Scheduling timers

History

A timer in Node.js is an internal construct that calls a given function after a certain period of time. When a timer's function is called varies depending on which method was used to create the timer and what other work the Node.js event loop is doing.

setImmediate(callback, ...args?): Immediate
Attributes
callback:Function
The function to call at the end of this turn of the Node.js Event Loop
...args:any
Optional arguments to pass when the callback is called.
Returns:Immediate
for use with clearImmediate()

Schedules the "immediate" execution of the callback after I/O events' callbacks.

When multiple calls to setImmediate() are made, the callback functions are queued for execution in the order in which they are created. The entire callback queue is processed every event loop iteration. If an immediate timer is queued from inside an executing callback, that timer will not be triggered until the next event loop iteration.

If callback is not a function, a TypeError will be thrown.

This method has a custom variant for promises that is available using timersPromises.setImmediate().

setInterval(callback, delay?, ...args?): Timeout
Attributes
callback:Function
The function to call when the timer elapses.
delay?:number
The number of milliseconds to wait before calling the callback. Default: 1.
...args:any
Optional arguments to pass when the callback is called.
Returns:Timeout
for use with clearInterval()

Schedules repeated execution of callback every delay milliseconds.

When delay is larger than 2147483647 or less than 1 or NaN, the delay will be set to 1. Non-integer delays are truncated to an integer.

If callback is not a function, a TypeError will be thrown.

This method has a custom variant for promises that is available using timersPromises.setInterval().

setTimeout(callback, delay?, ...args?): Timeout
Attributes
callback:Function
The function to call when the timer elapses.
delay?:number
The number of milliseconds to wait before calling the callback. Default: 1.
...args:any
Optional arguments to pass when the callback is called.
Returns:Timeout
for use with clearTimeout()

Schedules execution of a one-time callback after delay milliseconds.

The callback will likely not be invoked in precisely delay milliseconds. Node.js makes no guarantees about the exact timing of when callbacks will fire, nor of their ordering. The callback will be called as close as possible to the time specified.

When delay is larger than 2147483647 or less than 1 or NaN, the delay will be set to 1. Non-integer delays are truncated to an integer.

If callback is not a function, a TypeError will be thrown.

This method has a custom variant for promises that is available using timersPromises.setTimeout().