2012-02-27 20:09:34 +01:00
|
|
|
# Timers
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 3 - Locked
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
All of the timer functions are globals. You do not need to `require()`
|
|
|
|
this module in order to use them.
|
|
|
|
|
2015-11-04 18:35:20 +01:00
|
|
|
## clearImmediate(immediateObject)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:35:20 +01:00
|
|
|
Stops an immediate from triggering.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:35:20 +01:00
|
|
|
## clearInterval(intervalObject)
|
2011-09-14 05:02:54 +02:00
|
|
|
|
2015-11-04 18:35:20 +01:00
|
|
|
Stops an interval from triggering.
|
2015-10-25 02:56:49 +01:00
|
|
|
|
2014-02-09 10:37:55 +01:00
|
|
|
## clearTimeout(timeoutObject)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Prevents a timeout from triggering.
|
|
|
|
|
2015-11-04 18:35:20 +01:00
|
|
|
## ref()
|
|
|
|
|
|
|
|
If you had previously `unref()`d a timer you can call `ref()` to explicitly
|
|
|
|
request the timer hold the program open. If the timer is already `ref`d calling
|
|
|
|
`ref` again will have no effect.
|
|
|
|
|
|
|
|
Returns the timer.
|
|
|
|
|
|
|
|
## setImmediate(callback[, arg][, ...])
|
|
|
|
|
|
|
|
To schedule the "immediate" execution of `callback` after I/O events
|
2015-11-28 00:30:32 +01:00
|
|
|
callbacks and before [`setTimeout`][] and [`setInterval`][]. Returns an
|
2015-11-04 18:35:20 +01:00
|
|
|
`immediateObject` for possible use with `clearImmediate()`. Optionally you
|
|
|
|
can also pass arguments to the callback.
|
|
|
|
|
|
|
|
Callbacks for immediates are queued in the order in which they were created.
|
|
|
|
The entire callback queue is processed every event loop iteration. If you queue
|
|
|
|
an immediate from inside an executing callback, that immediate won't fire
|
|
|
|
until the next event loop iteration.
|
|
|
|
|
2014-09-30 01:32:34 +02:00
|
|
|
## setInterval(callback, delay[, arg][, ...])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
To schedule the repeated execution of `callback` every `delay` milliseconds.
|
2014-02-09 10:37:55 +01:00
|
|
|
Returns a `intervalObject` for possible use with `clearInterval()`. Optionally
|
2010-10-28 14:18:16 +02:00
|
|
|
you can also pass arguments to the callback.
|
|
|
|
|
2015-10-25 02:56:49 +01:00
|
|
|
To follow browser behavior, when using delays larger than 2147483647
|
|
|
|
milliseconds (approximately 25 days) or less than 1, Node.js will use 1 as the
|
|
|
|
`delay`.
|
|
|
|
|
2015-11-04 18:35:20 +01:00
|
|
|
## setTimeout(callback, delay[, arg][, ...])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:35:20 +01:00
|
|
|
To schedule execution of a one-time `callback` after `delay` milliseconds. Returns a
|
|
|
|
`timeoutObject` for possible use with `clearTimeout()`. Optionally you can
|
|
|
|
also pass arguments to the callback.
|
|
|
|
|
|
|
|
It is important to note that your callback will probably not be called in exactly
|
|
|
|
`delay` milliseconds - Node.js makes no guarantees about the exact timing of when
|
|
|
|
the callback will fire, nor of the ordering things will fire in. The callback will
|
|
|
|
be called as close as possible to the time specified.
|
|
|
|
|
|
|
|
To follow browser behavior, when using delays larger than 2147483647
|
|
|
|
milliseconds (approximately 25 days) or less than 1, the timeout is executed
|
|
|
|
immediately, as if the `delay` was set to 1.
|
2012-07-13 21:08:32 +02:00
|
|
|
|
|
|
|
## unref()
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
The opaque value returned by [`setTimeout`][] and [`setInterval`][] also has the method
|
2012-07-13 21:08:32 +02:00
|
|
|
`timer.unref()` which will allow you to create a timer that is active but if
|
2015-02-12 07:13:54 +01:00
|
|
|
it is the only item left in the event loop, it won't keep the program running.
|
2012-07-13 21:08:32 +02:00
|
|
|
If the timer is already `unref`d calling `unref` again will have no effect.
|
|
|
|
|
|
|
|
In the case of `setTimeout` when you `unref` you create a separate timer that
|
|
|
|
will wakeup the event loop, creating too many of these may adversely effect
|
|
|
|
event loop performance -- use wisely.
|
|
|
|
|
2015-09-13 17:21:51 +02:00
|
|
|
Returns the timer.
|
2015-11-28 00:30:32 +01:00
|
|
|
|
|
|
|
[`setInterval`]: timers.html#timers_setinterval_callback_delay_arg
|
|
|
|
[`setTimeout`]: timers.html#timers_settimeout_callback_delay_arg
|