|
2 | 2 |
|
3 | 3 | Stability: 3 - Locked
|
4 | 4 |
|
5 |
| -All of the timer functions are globals. You do not need to `require()` |
6 |
| -this module in order to use them. |
| 5 | +The `timer` module exposes a global API for scheduling functions to |
| 6 | +be called at some future period of time. Because the timer functions are |
| 7 | +globals, there is no need to call `require('timers')` to use the API. |
7 | 8 |
|
8 |
| -## clearImmediate(immediateObject) |
| 9 | +The timer functions within Node.js implement a similar API as the timers API |
| 10 | +provided by Web Browsers but use a different internal implementation that is |
| 11 | +built around [the Node.js Event Loop][]. |
9 | 12 |
|
10 |
| -Stops an `immediateObject`, as created by [`setImmediate`][], from triggering. |
| 13 | +## Class: Immediate |
11 | 14 |
|
12 |
| -## clearInterval(intervalObject) |
| 15 | +This object is created internally and is returned from [`setImmediate()`][]. It |
| 16 | +can be passed to [`clearImmediate()`][] in order to cancel the scheduled |
| 17 | +actions. |
13 | 18 |
|
14 |
| -Stops an `intervalObject`, as created by [`setInterval`][], from triggering. |
| 19 | +## Class: Timeout |
15 | 20 |
|
16 |
| -## clearTimeout(timeoutObject) |
| 21 | +This object is created internally and is returned from [`setTimeout()`][] and |
| 22 | +[`setInterval()`][]. It can be passed to [`clearTimeout`][] or |
| 23 | +[`clearInterval()`][] (respectively) in order to cancel the scheduled actions. |
17 | 24 |
|
18 |
| -Prevents a `timeoutObject`, as created by [`setTimeout`][], from triggering. |
| 25 | +By default, when a timer is scheduled using either [`setTimeout`] or |
| 26 | +[`setInterval()`][], the Node.js event loop will continue running as long as the |
| 27 | +timer is active. Each of the `Timeout` objects returned by these functions |
| 28 | +export both `timeout.ref()` and `timeout.unref()` functions that can be used to |
| 29 | +control this default behavior. |
19 | 30 |
|
20 |
| -## ref() |
| 31 | +### timeout.ref() |
21 | 32 |
|
22 |
| -If a timer was previously `unref()`d, then `ref()` can be called to explicitly |
23 |
| -request the timer hold the program open. If the timer is already `ref`d calling |
24 |
| -`ref` again will have no effect. |
| 33 | +When called, requests that the Node.js event loop *not* exit so long as the |
| 34 | +`Timeout` is active. Calling `timeout.ref()` multiple times will have no effect. |
25 | 35 |
|
26 |
| -Returns the timer. |
| 36 | +*Note*: By default, all `Timeout` objects are "ref'd", making it normally |
| 37 | +unnecessary to call `timeout.ref()` unless `timeout.unref()` had been called |
| 38 | +previously. |
27 | 39 |
|
28 |
| -## setImmediate(callback[, arg][, ...]) |
| 40 | +Returns a reference to the `Timeout`. |
29 | 41 |
|
30 |
| -Schedules "immediate" execution of `callback` after I/O events' |
31 |
| -callbacks and before timers set by [`setTimeout`][] and [`setInterval`][] are |
32 |
| -triggered. Returns an `immediateObject` for possible use with |
33 |
| -[`clearImmediate`][]. Additional optional arguments may be passed to the |
34 |
| -callback. |
| 42 | +### timeout.unref() |
35 | 43 |
|
36 |
| -Callbacks for immediates are queued in the order in which they were created. |
37 |
| -The entire callback queue is processed every event loop iteration. If an |
38 |
| -immediate is queued from inside an executing callback, that immediate won't fire |
39 |
| -until the next event loop iteration. |
| 44 | +When called, the active `Timeout` object will not require the Node.js event loop |
| 45 | +to remain active. If there is no other activity keeping the event loop running, |
| 46 | +the process may exit before the `Timeout` object's callback is invoked. Calling |
| 47 | +`timout.unref()` multiple times will have no effect. |
40 | 48 |
|
41 |
| -If `callback` is not a function `setImmediate()` will throw immediately. |
| 49 | +*Note*: Calling `timout.unref()` creates an internal timer that will wake the |
| 50 | +Node.js event loop. Creating too many of these can adversely impact performance |
| 51 | +of the Node.js application. |
42 | 52 |
|
43 |
| -## setInterval(callback, delay[, arg][, ...]) |
| 53 | +Returns a reference to the `Timeout`. |
| 54 | + |
| 55 | +## Scheduling Timers |
| 56 | + |
| 57 | +A timer in Node.js is an internal construct that calls a given function after |
| 58 | +a certain period of time. When a timer's function is called varies depending on |
| 59 | +which method was used to create the timer and what other work the Node.js |
| 60 | +event loop is doing. |
| 61 | + |
| 62 | +### setImmediate(callback[, ...arg]) |
| 63 | + |
| 64 | +* `callback` {Function} The function to call at the end of this turn of |
| 65 | + [the Node.js Event Loop] |
| 66 | +* `[, ...arg]` Optional arguments to pass when the `callback` is called. |
| 67 | + |
| 68 | +Schedules the "immediate" execution of the `callback` after I/O events' |
| 69 | +callbacks and before timers created using [`setTimeout()`][] and |
| 70 | +[`setInterval()`][] are triggered. Returns an `Immediate` for use with |
| 71 | +[`clearImmediate()`][]. |
| 72 | + |
| 73 | +When multiple calls to `setImmediate()` are made, the `callback` functions are |
| 74 | +queued for execution in the order in which they are created. The entire callback |
| 75 | +queue is processed every event loop iteration. If an immediate timer is queued |
| 76 | +from inside an executing callback, that timer will not be triggered until the |
| 77 | +next event loop iteration. |
| 78 | + |
| 79 | +If `callback` is not a function, a [`TypeError`][] will be thrown. |
| 80 | + |
| 81 | +### setInterval(callback, delay[, ...arg]) |
| 82 | + |
| 83 | +* `callback` {Function} The function to call when the timer elapses. |
| 84 | +* `delay` {number} The number of milliseconds to wait before calling the |
| 85 | + `callback`. |
| 86 | +* `[, ...arg]` Optional arguments to pass when the `callback` is called. |
44 | 87 |
|
45 | 88 | Schedules repeated execution of `callback` every `delay` milliseconds.
|
46 |
| -Returns a `intervalObject` for possible use with [`clearInterval`][]. Additional |
47 |
| -optional arguments may be passed to the callback. |
| 89 | +Returns a `Timeout` for use with [`clearInterval()`][]. |
48 | 90 |
|
49 |
| -To follow browser behavior, when using delays larger than 2147483647 |
50 |
| -milliseconds (approximately 25 days) or less than 1, Node.js will use 1 as the |
51 |
| -`delay`. |
| 91 | +When `delay` is larger than `2147483647` or less than `1`, the `delay` will be |
| 92 | +set to `1`. |
52 | 93 |
|
53 |
| -If `callback` is not a function `setInterval()` will throw immediately. |
| 94 | +If `callback` is not a function, a [`TypeError`][] will be thrown. |
54 | 95 |
|
55 |
| -## setTimeout(callback, delay[, arg][, ...]) |
| 96 | +### setTimeout(callback, delay[, ...arg]) |
| 97 | + |
| 98 | +* `callback` {Function} The function to call when the timer elapses. |
| 99 | +* `delay` {number} The number of milliseconds to wait before calling the |
| 100 | + `callback`. |
| 101 | +* `[, ...arg]` Optional arguments to pass when the `callback` is called. |
56 | 102 |
|
57 | 103 | Schedules execution of a one-time `callback` after `delay` milliseconds.
|
58 |
| -Returns a `timeoutObject` for possible use with [`clearTimeout`][]. Additional |
59 |
| -optional arguments may be passed to the callback. |
| 104 | +Returns a `Timeout` for use with [`clearTimeout()`][]. |
60 | 105 |
|
61 |
| -The callback will likely not be invoked in precisely `delay` milliseconds. |
| 106 | +The `callback` will likely not be invoked in precisely `delay` milliseconds. |
62 | 107 | Node.js makes no guarantees about the exact timing of when callbacks will fire,
|
63 | 108 | nor of their ordering. The callback will be called as close as possible to the
|
64 | 109 | time specified.
|
65 | 110 |
|
66 |
| -To follow browser behavior, when using delays larger than 2147483647 |
67 |
| -milliseconds (approximately 25 days) or less than 1, the timeout is executed |
68 |
| -immediately, as if the `delay` was set to 1. |
| 111 | +*Note*: When `delay` is larger than `2147483647` or less than `1`, the `delay` |
| 112 | +will be set to `1`. |
| 113 | + |
| 114 | +If `callback` is not a function, a [`TypeError`][] will be thrown. |
| 115 | + |
| 116 | +## Cancelling Timers |
| 117 | + |
| 118 | +The [`setImmediate()`][], [`setInterval()`][], and [`setTimeout()`][] methods |
| 119 | +each return objects that represent the scheduled timers. These can be used to |
| 120 | +cancel the timer and prevent it from triggering. |
| 121 | + |
| 122 | +### clearImmediate(immediate) |
| 123 | + |
| 124 | +* `immediate` {Immediate} An `Immediate` object as returned by |
| 125 | + [`setImmediate()`][]. |
| 126 | + |
| 127 | +Cancels an `Immediate` object created by [`setImmediate()`][]. |
| 128 | + |
| 129 | +### clearInterval(timeout) |
| 130 | + |
| 131 | +* `timeout` {Timeout} A `Timeout` object as returned by [`setInterval()`][]. |
69 | 132 |
|
70 |
| -If `callback` is not a function `setTimeout()` will throw immediately. |
| 133 | +Cancels a `Timeout` object created by [`setInterval()`][]. |
71 | 134 |
|
72 |
| -## unref() |
| 135 | +### clearTimeout(timeout) |
73 | 136 |
|
74 |
| -The opaque value returned by [`setTimeout`][] and [`setInterval`][] also has the |
75 |
| -method `timer.unref()` which allows the creation of a timer that is active but |
76 |
| -if it is the only item left in the event loop, it won't keep the program |
77 |
| -running. If the timer is already `unref`d calling `unref` again will have no |
78 |
| -effect. |
| 137 | +* `timeout` {Timeout} A `Timeout` object as returned by [`setTimeout()`][]. |
79 | 138 |
|
80 |
| -In the case of [`setTimeout`][], `unref` creates a separate timer that will |
81 |
| -wakeup the event loop, creating too many of these may adversely effect event |
82 |
| -loop performance -- use wisely. |
| 139 | +Cancels a `Timeout` object created by [`setTimeout()`][]. |
83 | 140 |
|
84 |
| -Returns the timer. |
85 | 141 |
|
86 |
| -[`clearImmediate`]: timers.html#timers_clearimmediate_immediateobject |
87 |
| -[`clearInterval`]: timers.html#timers_clearinterval_intervalobject |
88 |
| -[`clearTimeout`]: timers.html#timers_cleartimeout_timeoutobject |
89 |
| -[`setImmediate`]: timers.html#timers_setimmediate_callback_arg |
90 |
| -[`setInterval`]: timers.html#timers_setinterval_callback_delay_arg |
91 |
| -[`setTimeout`]: timers.html#timers_settimeout_callback_delay_arg |
| 142 | +[the Node.js Event Loop]: https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md |
| 143 | +[`TypeError`]: errors.html#errors_class_typerror |
| 144 | +[`clearImmediate()`]: timers.html#timers_clearimmediate_immediate |
| 145 | +[`clearInterval()`]: timers.html#timers_clearinterval_timeout |
| 146 | +[`clearTimeout()`]: timers.html#timers_cleartimeout_timeout |
| 147 | +[`setImmediate()`]: timers.html#timers_setimmediate_callback_arg |
| 148 | +[`setInterval()`]: timers.html#timers_setinterval_callback_delay_arg |
| 149 | +[`setTimeout()`]: timers.html#timers_settimeout_callback_delay_arg |
0 commit comments