2012-02-27 20:09:34 +01:00
|
|
|
# Timers
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
2017-02-27 16:32:23 +01:00
|
|
|
> Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
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.
|
2012-02-27 20:09:34 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
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
|
2019-01-21 21:08:35 +01:00
|
|
|
built around the Node.js [Event Loop][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
## Class: `Immediate`
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
This object is created internally and is returned from [`setImmediate()`][]. It
|
|
|
|
can be passed to [`clearImmediate()`][] in order to cancel the scheduled
|
|
|
|
actions.
|
2011-09-14 05:02:54 +02:00
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
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.
|
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `immediate.hasRef()`
|
2018-05-23 00:01:53 +02:00
|
|
|
<!-- YAML
|
2018-10-03 01:01:19 +02:00
|
|
|
added: v11.0.0
|
2018-05-23 00:01:53 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
If true, the `Immediate` object will keep the Node.js event loop active.
|
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `immediate.ref()`
|
2018-01-13 22:51:28 +01:00
|
|
|
<!-- YAML
|
2018-02-27 19:21:55 +01:00
|
|
|
added: v9.7.0
|
2018-01-13 22:51:28 +01:00
|
|
|
-->
|
|
|
|
|
2018-04-26 01:49:11 +02:00
|
|
|
* Returns: {Immediate} a reference to `immediate`
|
2018-04-11 20:07:14 +02:00
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
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.
|
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
By default, all `Immediate` objects are "ref'ed", making it normally unnecessary
|
2018-02-06 06:55:16 +01:00
|
|
|
to call `immediate.ref()` unless `immediate.unref()` had been called previously.
|
2018-01-13 22:51:28 +01:00
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `immediate.unref()`
|
2018-01-13 22:51:28 +01:00
|
|
|
<!-- YAML
|
2018-02-27 19:21:55 +01:00
|
|
|
added: v9.7.0
|
2018-01-13 22:51:28 +01:00
|
|
|
-->
|
|
|
|
|
2018-04-26 01:49:11 +02:00
|
|
|
* Returns: {Immediate} a reference to `immediate`
|
2018-04-11 20:07:14 +02:00
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
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.
|
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
## Class: `Timeout`
|
2015-10-25 02:56:49 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
This object is created internally and is returned from [`setTimeout()`][] and
|
2018-04-11 17:49:07 +02:00
|
|
|
[`setInterval()`][]. It can be passed to either [`clearTimeout()`][] or
|
|
|
|
[`clearInterval()`][] in order to cancel the scheduled actions.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-09-11 03:42:00 +02:00
|
|
|
By default, when a timer is scheduled using either [`setTimeout()`][] or
|
2016-05-23 23:12:20 +02:00
|
|
|
[`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.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `timeout.hasRef()`
|
2018-05-23 00:01:53 +02:00
|
|
|
<!-- YAML
|
2018-10-03 01:01:19 +02:00
|
|
|
added: v11.0.0
|
2018-05-23 00:01:53 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Returns: {boolean}
|
|
|
|
|
|
|
|
If true, the `Timeout` object will keep the Node.js event loop active.
|
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `timeout.ref()`
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2018-04-26 01:49:11 +02:00
|
|
|
* Returns: {Timeout} a reference to `timeout`
|
2018-04-11 20:07:14 +02:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
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.
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2018-04-09 18:30:22 +02:00
|
|
|
By default, all `Timeout` objects are "ref'ed", making it normally unnecessary
|
2018-02-06 06:55:16 +01:00
|
|
|
to call `timeout.ref()` unless `timeout.unref()` had been called previously.
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `timeout.refresh()`
|
2018-04-25 18:45:34 +02:00
|
|
|
<!-- YAML
|
2018-05-14 20:01:36 +02:00
|
|
|
added: v10.2.0
|
2018-04-25 18:45:34 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Returns: {Timeout} a reference to `timeout`
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `timeout.unref()`
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2018-04-26 01:49:11 +02:00
|
|
|
* Returns: {Timeout} a reference to `timeout`
|
2018-04-11 20:07:14 +02:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
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
|
2016-08-23 08:45:49 +02:00
|
|
|
`timeout.unref()` multiple times will have no effect.
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
Calling `timeout.unref()` creates an internal timer that will wake the Node.js
|
|
|
|
event loop. Creating too many of these can adversely impact performance
|
2016-05-23 23:12:20 +02:00
|
|
|
of the Node.js application.
|
2015-12-20 12:30:04 +01:00
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
## Scheduling timers
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `setImmediate(callback[, ...args])`
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
* `callback` {Function} The function to call at the end of this turn of
|
2019-10-02 06:31:57 +02:00
|
|
|
the Node.js [Event Loop][]
|
2016-08-30 07:35:03 +02:00
|
|
|
* `...args` {any} Optional arguments to pass when the `callback` is called.
|
2018-04-26 01:49:11 +02:00
|
|
|
* Returns: {Immediate} for use with [`clearImmediate()`][]
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
Schedules the "immediate" execution of the `callback` after I/O events'
|
2018-04-26 01:49:11 +02:00
|
|
|
callbacks.
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
This method has a custom variant for promises that is available using
|
2017-04-14 21:42:47 +02:00
|
|
|
[`util.promisify()`][]:
|
|
|
|
|
|
|
|
```js
|
|
|
|
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.
|
|
|
|
});
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Or with async function
|
2017-04-14 21:42:47 +02:00
|
|
|
async function timerExample() {
|
|
|
|
console.log('Before I/O callbacks');
|
|
|
|
await setImmediatePromise();
|
|
|
|
console.log('After I/O callbacks');
|
|
|
|
}
|
|
|
|
timerExample();
|
|
|
|
```
|
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `setInterval(callback, delay[, ...args])`
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
* `callback` {Function} The function to call when the timer elapses.
|
|
|
|
* `delay` {number} The number of milliseconds to wait before calling the
|
|
|
|
`callback`.
|
2016-08-30 07:35:03 +02:00
|
|
|
* `...args` {any} Optional arguments to pass when the `callback` is called.
|
2018-04-26 01:49:11 +02:00
|
|
|
* Returns: {Timeout} for use with [`clearInterval()`][]
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
Schedules repeated execution of `callback` every `delay` milliseconds.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
When `delay` is larger than `2147483647` or less than `1`, the `delay` will be
|
2018-12-04 02:41:58 +01:00
|
|
|
set to `1`. Non-integer delays are truncated to an integer.
|
2015-10-25 02:56:49 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
If `callback` is not a function, a [`TypeError`][] will be thrown.
|
2015-12-20 12:30:04 +01:00
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `setTimeout(callback, delay[, ...args])`
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
* `callback` {Function} The function to call when the timer elapses.
|
|
|
|
* `delay` {number} The number of milliseconds to wait before calling the
|
|
|
|
`callback`.
|
2016-08-30 07:35:03 +02:00
|
|
|
* `...args` {any} Optional arguments to pass when the `callback` is called.
|
2018-04-26 01:49:11 +02:00
|
|
|
* Returns: {Timeout} for use with [`clearTimeout()`][]
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
Schedules execution of a one-time `callback` after `delay` milliseconds.
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
The `callback` will likely not be invoked in precisely `delay` milliseconds.
|
2015-12-27 03:39:16 +01:00
|
|
|
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.
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
When `delay` is larger than `2147483647` or less than `1`, the `delay`
|
2018-12-04 02:41:58 +01:00
|
|
|
will be set to `1`. Non-integer delays are truncated to an integer.
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
If `callback` is not a function, a [`TypeError`][] will be thrown.
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
This method has a custom variant for promises that is available using
|
2017-04-14 21:42:47 +02:00
|
|
|
[`util.promisify()`][]:
|
|
|
|
|
|
|
|
```js
|
|
|
|
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.
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2020-06-14 23:49:34 +02:00
|
|
|
## Cancelling timers
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2020-06-10 23:14:23 +02:00
|
|
|
For the promisified variants of [`setImmediate()`][] and [`setTimeout()`][],
|
|
|
|
an [`AbortController`][] may be used to cancel the timer. When canceled, the
|
|
|
|
returned Promises will be rejected with an `'AbortError'`.
|
|
|
|
|
|
|
|
For `setImmediate()`:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
const setImmediatePromise = util.promisify(setImmediate);
|
|
|
|
|
|
|
|
const ac = new AbortController();
|
|
|
|
const signal = ac.signal;
|
|
|
|
|
|
|
|
setImmediatePromise('foobar', { signal })
|
|
|
|
.then(console.log)
|
|
|
|
.catch((err) => {
|
|
|
|
if (err.message === 'AbortError')
|
|
|
|
console.log('The immediate was aborted');
|
|
|
|
});
|
|
|
|
|
|
|
|
ac.abort();
|
|
|
|
```
|
|
|
|
|
|
|
|
For `setTimeout()`:
|
|
|
|
|
|
|
|
```js
|
|
|
|
const util = require('util');
|
|
|
|
const setTimeoutPromise = util.promisify(setTimeout);
|
|
|
|
|
|
|
|
const ac = new AbortController();
|
|
|
|
const signal = ac.signal;
|
|
|
|
|
|
|
|
setTimeoutPromise(1000, 'foobar', { signal })
|
|
|
|
.then(console.log)
|
|
|
|
.catch((err) => {
|
|
|
|
if (err.message === 'AbortError')
|
|
|
|
console.log('The timeout was aborted');
|
|
|
|
});
|
|
|
|
|
|
|
|
ac.abort();
|
|
|
|
```
|
2017-04-14 21:42:47 +02:00
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `clearImmediate(immediate)`
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
* `immediate` {Immediate} An `Immediate` object as returned by
|
|
|
|
[`setImmediate()`][].
|
|
|
|
|
|
|
|
Cancels an `Immediate` object created by [`setImmediate()`][].
|
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `clearInterval(timeout)`
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
* `timeout` {Timeout} A `Timeout` object as returned by [`setInterval()`][].
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
Cancels a `Timeout` object created by [`setInterval()`][].
|
2015-12-20 12:30:04 +01:00
|
|
|
|
2019-12-25 00:11:21 +01:00
|
|
|
### `clearTimeout(timeout)`
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.0.1
|
|
|
|
-->
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
* `timeout` {Timeout} A `Timeout` object as returned by [`setTimeout()`][].
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
Cancels a `Timeout` object created by [`setTimeout()`][].
|
2012-07-13 21:08:32 +02:00
|
|
|
|
2020-06-18 22:22:17 +02:00
|
|
|
## Timers Promises API
|
|
|
|
|
|
|
|
> Stability: 1 - Experimental
|
|
|
|
|
|
|
|
The `timers/promises` API provides an alternative set of timer functions
|
|
|
|
that return `Promise` objects. The API is accessible via
|
|
|
|
`require('timers/promises')`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
const timersPromises = require('timers/promises');
|
|
|
|
```
|
|
|
|
|
2020-06-23 16:12:52 +02:00
|
|
|
### `timersPromises.setTimeout(delay\[, value\[, options\]\])`
|
2020-06-18 22:22:17 +02:00
|
|
|
|
|
|
|
* `delay` {number} The number of milliseconds to wait before resolving the
|
|
|
|
`Promise`.
|
|
|
|
* `value` {any} A value with which the `Promise` is resolved.
|
|
|
|
* `options` {Object}
|
|
|
|
* `ref` {boolean} Set to `false` to indicate that the scheduled `Timeout`
|
|
|
|
should not require the Node.js event loop to remain active.
|
|
|
|
**Default**: `true`.
|
|
|
|
* `signal` {AbortSignal} An optional `AbortSignal` that can be used to
|
|
|
|
cancel the scheduled `Timeout`.
|
|
|
|
|
2020-06-23 16:12:52 +02:00
|
|
|
### `timersPromises.setImmediate(\[value\[, options\]\])`
|
2020-06-18 22:22:17 +02:00
|
|
|
|
|
|
|
* `value` {any} A value with which the `Promise` is resolved.
|
|
|
|
* `options` {Object}
|
|
|
|
* `ref` {boolean} Set to `false` to indicate that the scheduled `Immediate`
|
|
|
|
should not require the Node.js event loop to remain active.
|
|
|
|
**Default**: `true`.
|
|
|
|
* `signal` {AbortSignal} An optional `AbortSignal` that can be used to
|
|
|
|
cancel the scheduled `Immediate`.
|
|
|
|
|
2019-01-21 21:08:35 +01:00
|
|
|
[Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout
|
2020-06-10 23:14:23 +02:00
|
|
|
[`AbortController`]: globals.html#globals_class_abortcontroller
|
2016-07-09 07:13:09 +02:00
|
|
|
[`TypeError`]: errors.html#errors_class_typeerror
|
2016-05-23 23:12:20 +02:00
|
|
|
[`clearImmediate()`]: timers.html#timers_clearimmediate_immediate
|
|
|
|
[`clearInterval()`]: timers.html#timers_clearinterval_timeout
|
|
|
|
[`clearTimeout()`]: timers.html#timers_cleartimeout_timeout
|
2016-08-30 07:35:03 +02:00
|
|
|
[`setImmediate()`]: timers.html#timers_setimmediate_callback_args
|
|
|
|
[`setInterval()`]: timers.html#timers_setinterval_callback_delay_args
|
|
|
|
[`setTimeout()`]: timers.html#timers_settimeout_callback_delay_args
|
2017-04-14 21:42:47 +02:00
|
|
|
[`util.promisify()`]: util.html#util_util_promisify_original
|