/usr/share/doc/nodejs/api
NameSizeModeActions
assets/-0755rm
addons.html851720644editdlrm
addons.json.gz111880644editdlrm
addons.md410130644editdlrm
all.html45980480644editdlrm
all.json.gz6816430644editdlrm
assert.html1106650644editdlrm
assert.json.gz120070644editdlrm
assert.md471470644editdlrm
async_hooks.html835990644editdlrm
async_hooks.json.gz140850644editdlrm
async_hooks.md388880644editdlrm
buffer.html2457620644editdlrm
buffer.json.gz241050644editdlrm
buffer.md1050440644editdlrm
child_process.html1367670644editdlrm
child_process.json.gz193990644editdlrm
child_process.md629190644editdlrm
cli.html879360644editdlrm
cli.json.gz166150644editdlrm
cli.md422630644editdlrm
cluster.html601160644editdlrm
cluster.json.gz95570644editdlrm
cluster.md254260644editdlrm
console.html460930644editdlrm
console.json.gz60560644editdlrm
console.md164310644editdlrm
crypto.html2830430644editdlrm
crypto.json.gz321180644editdlrm
crypto.md1251980644editdlrm
debugger.html180890644editdlrm
debugger.json.gz28200644editdlrm
debugger.md61330644editdlrm
deprecations.html1385370644editdlrm
deprecations.json.gz184700644editdlrm
deprecations.md791200644editdlrm
dgram.html624250644editdlrm
dgram.json.gz96300644editdlrm
dgram.md277460644editdlrm
dns.html982890644editdlrm
dns.json.gz138380644editdlrm
dns.md407990644editdlrm
documentation.html109780644editdlrm
documentation.json33280644editdlrm
documentation.md22470644editdlrm
domain.html363010644editdlrm
domain.json.gz63550644editdlrm
domain.md155210644editdlrm
embedding.html181650644editdlrm
embedding.json.gz36250644editdlrm
embedding.md88110644editdlrm
errors.html1829820644editdlrm
errors.json.gz364790644editdlrm
errors.md817340644editdlrm
esm.html924900644editdlrm
esm.json.gz150540644editdlrm
esm.md484660644editdlrm
events.html759860644editdlrm
events.json.gz95610644editdlrm
events.md288230644editdlrm
fs.html4371790644editdlrm
fs.json.gz470950644editdlrm
fs.md1877350644editdlrm
globals.html197950644editdlrm
globals.json.gz25250644editdlrm
globals.md58380644editdlrm
http.html1643780644editdlrm
http.json.gz258880644editdlrm
http.md744650644editdlrm
http2.html2564510644editdlrm
http2.json.gz371570644editdlrm
http2.md1217220644editdlrm
https.html383450644editdlrm
https.json.gz48470644editdlrm
https.md139420644editdlrm
index.html100870644editdlrm
index.json540644editdlrm
index.md18250644editdlrm
inspector.html224330644editdlrm
inspector.json.gz33380644editdlrm
inspector.md68250644editdlrm
intl.html231540644editdlrm
intl.json.gz40380644editdlrm
intl.md110920644editdlrm
module.html217390644editdlrm
module.json.gz28210644editdlrm
module.md53410644editdlrm
modules.html613330644editdlrm
modules.json.gz118970644editdlrm
modules.md306100644editdlrm
n-api.html3403820644editdlrm
n-api.json.gz486820644editdlrm
n-api.md2062680644editdlrm
net.html951130644editdlrm
net.json.gz142650644editdlrm
net.md404730644editdlrm
os.html586580644editdlrm
os.json.gz85760644editdlrm
os.md346320644editdlrm
packages.html654830644editdlrm
packages.json.gz124010644editdlrm
packages.md365380644editdlrm
path.html393210644editdlrm
path.json.gz48730644editdlrm
path.md147500644editdlrm
perf_hooks.html562150644editdlrm
perf_hooks.json.gz69730644editdlrm
perf_hooks.md201160644editdlrm
policy.html159080644editdlrm
policy.json.gz29080644editdlrm
policy.md63240644editdlrm
process.html1693410644editdlrm
process.json.gz277340644editdlrm
process.md821620644editdlrm
punycode.html169040644editdlrm
punycode.json.gz19390644editdlrm
punycode.md40630644editdlrm
querystring.html193340644editdlrm
querystring.json.gz25190644editdlrm
querystring.md54230644editdlrm
readline.html584340644editdlrm
readline.json.gz88350644editdlrm
readline.md251330644editdlrm
repl.html541520644editdlrm
repl.json.gz100530644editdlrm
repl.md237540644editdlrm
report.html449410644editdlrm
report.json.gz62270644editdlrm
report.md182870644editdlrm
stream.html2005010644editdlrm
stream.json.gz344850644editdlrm
stream.md986190644editdlrm
string_decoder.html144150644editdlrm
string_decoder.json.gz15450644editdlrm
string_decoder.md29340644editdlrm
synopsis.html114750644editdlrm
synopsis.json30130644editdlrm
synopsis.md21290644editdlrm
timers.html274670644editdlrm
timers.json.gz36370644editdlrm
timers.md97380644editdlrm
tls.html1483810644editdlrm
tls.json.gz299880644editdlrm
tls.md813880644editdlrm
tracing.html195860644editdlrm
tracing.json.gz28290644editdlrm
tracing.md67560644editdlrm
tty.html269300644editdlrm
tty.json.gz37580644editdlrm
tty.md89890644editdlrm
url.html974950644editdlrm
url.json.gz136110644editdlrm
url.md448690644editdlrm
util.html1780810644editdlrm
util.json.gz205420644editdlrm
util.md712550644editdlrm
v8.html446880644editdlrm
v8.json.gz60820644editdlrm
v8.md155330644editdlrm
vm.html1033920644editdlrm
vm.json.gz140230644editdlrm
vm.md482930644editdlrm
wasi.html170880644editdlrm
wasi.json.gz28140644editdlrm
wasi.md59320644editdlrm
worker_threads.html708080644editdlrm
worker_threads.json.gz113260644editdlrm
worker_threads.md338830644editdlrm
zlib.html901240644editdlrm
zlib.json.gz105690644editdlrm
zlib.md356970644editdlrm
Edit: /usr/share/doc/nodejs/api/timers.html (27467B)
Timers | Node.js v12.20.0 Documentation

Node.js v12.20.0 Documentation


Timers#

Stability: 2 - Stable

Source Code: lib/timers.js

The 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('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 from setImmediate(). 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()#

If true, the Immediate object will keep the Node.js event loop active.

immediate.ref()#

When called, requests that the Node.js event loop not exit so long as the 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()#

When called, the active 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.

Class: Timeout#

This object is created internally and is returned from setTimeout() 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()#

If true, the Timeout object will keep the Node.js event loop active.

timeout.ref()#

When called, requests that the Node.js event loop not exit so long as the 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.refresh()#

Sets the timer's start time to the current time, and reschedules the timer to call its callback at the previously specified duration adjusted to the current time. This is useful for refreshing a timer without allocating a new JavaScript object.

Using this on a timer that has already called its callback will reactivate the timer.

timeout.unref()#

When called, the active 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.

Calling timeout.unref() creates an internal timer that will wake the Node.js event loop. Creating too many of these can adversely impact performance of the Node.js application.

timeout[Symbol.toPrimitive]()#

  • Returns: <integer> a number that can be used to reference this timeout

Coerce a Timeout to a primitive. The primitive can be used to clear the Timeout. The primitive can only be used in the same thread where the timeout was created. Therefore, to use it across worker_threads it must first be passed to the correct thread. This allows enhanced compatibility with browser setTimeout() and setInterval() implementations.

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])#

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 util.promisify():

const util = require('util');
const setImmediatePromise = util.promisify(setImmediate);

setImmediatePromise('foobar').then((value) => {
  // value === 'foobar' (passing values is optional)
  // This is executed after all I/O callbacks.
});

// Or with async function
async function timerExample() {
  console.log('Before I/O callbacks');
  await setImmediatePromise();
  console.log('After I/O callbacks');
}
timerExample();

setInterval(callback, delay[, ...args])#

  • callback <Function> The function to call when the timer elapses.
  • delay <number> The number of milliseconds to wait before calling the callback.
  • ...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, 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.

setTimeout(callback, delay[, ...args])#

  • callback <Function> The function to call when the timer elapses.
  • delay <number> The number of milliseconds to wait before calling the callback.
  • ...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, 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 util.promisify():

const util = require('util');
const setTimeoutPromise = util.promisify(setTimeout);

setTimeoutPromise(40, 'foobar').then((value) => {
  // value === 'foobar' (passing values is optional)
  // This is executed after about 40 milliseconds.
});

Cancelling timers#

The setImmediate(), 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.

It is not possible to cancel timers that were created using the promisified variants of setImmediate(), setTimeout().

clearImmediate(immediate)#

Cancels an Immediate object created by setImmediate().

clearInterval(timeout)#

Cancels a Timeout object created by setInterval().

clearTimeout(timeout)#

Cancels a Timeout object created by setTimeout().