Timers
Stability: 2 - Stable
timer module exposes a global API for scheduling functions to be called at some future period of time. Because the timer functions are globals, there is no need to call require('node:timers') to use the API.
The timer functions within Node.js implement a similar API as the timers API provided by Web Browsers but use a different internal implementation that is built around the Node.js Event Loop.
Class: Immediate
This object is created internally and is returned fromsetImmediate(). It can be passed to clearImmediate() in order to cancel the scheduled actions.
By default, when an immediate is scheduled, the Node.js event loop will continue running as long as the immediate is active. The Immediate object returned by setImmediate() exports both immediate.ref() and immediate.unref() functions that can be used to control this default behavior.
immediate.hasRef()
Added in: v11.0.0
If true, the Immediate object will keep the Node.js event loop active.
immediate.ref()
Added in: v9.7.0
A reference to immediate
Immediate is active. Calling immediate.ref() multiple times will have no effect.
By default, all Immediate objects are “ref’ed”, making it normally unnecessary to call immediate.ref() unless immediate.unref() had been called previously.
immediate.unref()
Added in: v9.7.0
A reference to immediate
Immediate object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the Immediate object’s callback is invoked. Calling immediate.unref() multiple times will have no effect.
immediateSymbol.dispose
Added in: v20.5.0, v18.18.0. No longer experimental in v24.2.0.
clearImmediate().
Class: Timeout
This object is created internally and is returned fromsetTimeout() and setInterval(). It can be passed to either clearTimeout() or clearInterval() in order to cancel the scheduled actions.
By default, when a timer is scheduled using either setTimeout() or setInterval(), the Node.js event loop will continue running as long as the timer is active. Each of the Timeout objects returned by these functions export both timeout.ref() and timeout.unref() functions that can be used to control this default behavior.
timeout.hasRef()
Added in: v11.0.0
If true, the Timeout object will keep the Node.js event loop active.
timeout.ref()
Added in: v0.9.1
A reference to timeout
Timeout is active. Calling timeout.ref() multiple times will have no effect.
By default, all Timeout objects are “ref’ed”, making it normally unnecessary to call timeout.ref() unless timeout.unref() had been called previously.
timeout.unref()
Added in: v0.9.1
A reference to timeout
Timeout object will not require the Node.js event loop to remain active. If there is no other activity keeping the event loop running, the process may exit before the Timeout object’s callback is invoked. Calling timeout.unref() multiple times will have no effect.
timeout.refresh()
Added in: v10.2.0
A reference to timeout
timeoutSymbol.dispose
Added in: v20.5.0, v18.18.0. No longer experimental in v24.2.0.
Scheduling Timers
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])
Added in: v0.9.1
The function to call at the end of this turn of the Node.js Event Loop
Optional arguments to pass when the callback is called
For use with clearImmediate()
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.
setInterval(callback[, delay[, …args]])
Added in: v0.0.1
The function to call when the timer elapses
The number of milliseconds to wait before calling the callback
Optional arguments to pass when the callback is called
For use with clearInterval()
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.
setTimeout(callback[, delay[, …args]])
Added in: v0.0.1
The function to call when the timer elapses
The number of milliseconds to wait before calling the callback
Optional arguments to pass when the callback is called
For use with clearTimeout()
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.
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.
Cancelling Timers
ThesetImmediate(), setInterval(), and setTimeout() methods each return objects that represent the scheduled timers. These can be used to cancel the timer and prevent it from triggering.
clearImmediate(immediate)
Added in: v0.9.1
An Immediate object as returned by setImmediate()
Immediate object created by setImmediate().
clearInterval(timeout)
Added in: v0.0.1
A Timeout object as returned by setInterval() or the primitive of the Timeout object
Timeout object created by setInterval().
clearTimeout(timeout)
Added in: v0.0.1
A Timeout object as returned by setTimeout() or the primitive of the Timeout object
Timeout object created by setTimeout().
Timers Promises API
Added in: v15.0.0. Graduated from experimental in v16.0.0.
timers/promises API provides an alternative set of timer functions that return Promise objects. The API is accessible via require('node:timers/promises').
timersPromises.setTimeout([delay[, value[, options]]])
Added in: v15.0.0
The number of milliseconds to wait before fulfilling the promise
A value with which the promise is fulfilled
A promise that resolves with the given value after the specified delay.
timersPromises.setImmediate([value[, options]])
Added in: v15.0.0
A value with which the promise is fulfilled
A promise that resolves with the given value.
timersPromises.setInterval([delay[, value[, options]]])
Added in: v15.9.0
The number of milliseconds to wait between iterations
A value with which the iterator returns
Returns an async iterator that generates values in an interval of delay ms.
Using AbortController with Timers
For the promisified variants ofsetImmediate() and setTimeout(), an AbortController may be used to cancel the timer. When canceled, the returned Promises will be rejected with an ‘AbortError’.
For setImmediate():
setTimeout():