2012-02-27 20:09:34 +01:00
|
|
|
# Timers
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 3 - Locked
|
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
|
|
|
|
built around [the Node.js Event Loop][].
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-05-23 23:12:20 +02: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
|
|
|
|
2016-05-23 23:12:20 +02: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
|
|
|
|
[`setInterval()`][]. It can be passed to [`clearTimeout`][] or
|
|
|
|
[`clearInterval()`][] (respectively) 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
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
### timeout.ref()
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2015-11-04 18:35:20 +01: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
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
*Note*: By default, all `Timeout` objects are "ref'd", making it normally
|
|
|
|
unnecessary to call `timeout.ref()` unless `timeout.unref()` had been called
|
|
|
|
previously.
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
Returns a reference to the `Timeout`.
|
2015-11-04 18:35:20 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
### timeout.unref()
|
2016-06-30 08:52:44 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.1
|
|
|
|
-->
|
2015-11-04 18:35:20 +01: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
|
|
|
|
2016-08-23 08:45:49 +02:00
|
|
|
*Note*: Calling `timeout.unref()` creates an internal timer that will wake the
|
2016-05-23 23:12:20 +02:00
|
|
|
Node.js event loop. Creating too many of these can adversely impact performance
|
|
|
|
of the Node.js application.
|
2015-12-20 12:30:04 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
Returns a reference to the `Timeout`.
|
|
|
|
|
|
|
|
## 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.
|
|
|
|
|
2016-08-30 07:35:03 +02: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
|
|
|
|
[the Node.js Event Loop]
|
2016-08-30 07:35:03 +02:00
|
|
|
* `...args` {any} Optional arguments to pass when the `callback` is called.
|
2016-05-23 23:12:20 +02:00
|
|
|
|
|
|
|
Schedules the "immediate" execution of the `callback` after I/O events'
|
|
|
|
callbacks and before timers created using [`setTimeout()`][] and
|
|
|
|
[`setInterval()`][] are triggered. Returns an `Immediate` for use with
|
|
|
|
[`clearImmediate()`][].
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2016-08-30 07:35:03 +02: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.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2016-03-22 00:03:41 +01:00
|
|
|
Schedules repeated execution of `callback` every `delay` milliseconds.
|
2016-05-23 23:12:20 +02:00
|
|
|
Returns a `Timeout` for use with [`clearInterval()`][].
|
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
|
|
|
|
set to `1`.
|
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
|
|
|
|
2016-08-30 07:35:03 +02: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.
|
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.
|
2016-05-23 23:12:20 +02:00
|
|
|
Returns a `Timeout` for use with [`clearTimeout()`][].
|
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
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
*Note*: When `delay` is larger than `2147483647` or less than `1`, the `delay`
|
|
|
|
will be set to `1`.
|
|
|
|
|
|
|
|
If `callback` is not a function, a [`TypeError`][] will be thrown.
|
|
|
|
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
### 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()`][].
|
|
|
|
|
|
|
|
### 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
|
|
|
|
2016-05-23 23:12:20 +02: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
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
|
2016-05-23 23:12:20 +02:00
|
|
|
[the Node.js Event Loop]: https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md
|
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
|