Skip to content

Commit f26db5b

Browse files
committed
doc: add timer classes
The timers returned by `setTimeout` and friends are actually instances of `Timeout` and `Immediate`. Documenting them as such, so that the `ref` and `unref` methods can be identified as methods on `Timeout` objects. Sparked by discussion in nodejs#5792
1 parent 7c7e50f commit f26db5b

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

doc/api/timers.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,56 @@
55
All of the timer functions are globals. You do not need to `require()`
66
this module in order to use them.
77

8-
## clearImmediate(immediateObject)
8+
## Class: Immediate
99

10-
Stops an `immediateObject`, as created by [`setImmediate`][], from triggering.
10+
This object is created internally and is returned from [`setImmediate`][]. It
11+
can be passed to [`clearImmediate`] in order to cancel the scheduled actions.
1112

12-
## clearInterval(intervalObject)
13+
## Class: Timeout
1314

14-
Stops an `intervalObject`, as created by [`setInterval`][], from triggering.
15+
This object is created internally and is returned from [`setTimeout`][] and
16+
[`setInterval`][]. It can be passed to [`clearTimeout`][] or [`clearInterval`][]
17+
respectively in order to cancel the scheduled actions.
1518

16-
## clearTimeout(timeoutObject)
19+
### ref()
1720

18-
Prevents a `timeoutObject`, as created by [`setTimeout`][], from triggering.
21+
If a `Timeout` was previously `unref()`d, then `ref()` can be called to
22+
explicitly request the `Timeout` hold the program open. If the `Timeout` is
23+
already `ref`d calling `ref` again will have no effect.
1924

20-
## ref()
25+
Returns the `Timeout`.
2126

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.
27+
### unref()
2528

26-
Returns the timer.
29+
The `Timeout` returned by [`setTimeout`][] and [`setInterval`][] also has the
30+
method `timeout.unref()` which allows the creation of a `Timeout` that is active
31+
but if it is the only item left in the event loop, it won't keep the program
32+
running. If the `Timeout` is already `unref`d calling `unref` again will have no
33+
effect.
34+
35+
In the case of [`setTimeout`][], `unref` creates a separate underlying timer
36+
handle that will wakeup the event loop, creating too many of these may adversely
37+
effect event loop performance -- use wisely.
38+
39+
Returns the `Timeout`.
40+
41+
## clearImmediate(immediate)
42+
43+
Stops an `Immediate`, as created by [`setImmediate`][], from triggering.
44+
45+
## clearInterval(timeout)
46+
47+
Stops a `Timeout`, as created by [`setInterval`][], from triggering.
48+
49+
## clearTimeout(timeout)
50+
51+
Stops a `Timeout`, as created by [`setTimeout`][], from triggering.
2752

2853
## setImmediate(callback[, arg][, ...])
2954

3055
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
56+
callbacks and before timers (`Timeout`s) set by [`setTimeout`][] and
57+
[`setInterval`][] are triggered. Returns an `Immediate` for possible use with
3358
[`clearImmediate`][]. Additional optional arguments may be passed to the
3459
callback.
3560

@@ -43,7 +68,7 @@ If `callback` is not a function `setImmediate()` will throw immediately.
4368
## setInterval(callback, delay[, arg][, ...])
4469

4570
Schedules repeated execution of `callback` every `delay` milliseconds.
46-
Returns a `intervalObject` for possible use with [`clearInterval`][]. Additional
71+
Returns a `Timeout` for possible use with [`clearInterval`][]. Additional
4772
optional arguments may be passed to the callback.
4873

4974
To follow browser behavior, when using delays larger than 2147483647
@@ -55,7 +80,7 @@ If `callback` is not a function `setInterval()` will throw immediately.
5580
## setTimeout(callback, delay[, arg][, ...])
5681

5782
Schedules execution of a one-time `callback` after `delay` milliseconds.
58-
Returns a `timeoutObject` for possible use with [`clearTimeout`][]. Additional
83+
Returns a `Timeout` for possible use with [`clearTimeout`][]. Additional
5984
optional arguments may be passed to the callback.
6085

6186
The callback will likely not be invoked in precisely `delay` milliseconds.
@@ -69,20 +94,6 @@ immediately, as if the `delay` was set to 1.
6994

7095
If `callback` is not a function `setTimeout()` will throw immediately.
7196

72-
## unref()
73-
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.
79-
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.
83-
84-
Returns the timer.
85-
8697
[`clearImmediate`]: timers.html#timers_clearimmediate_immediateobject
8798
[`clearInterval`]: timers.html#timers_clearinterval_intervalobject
8899
[`clearTimeout`]: timers.html#timers_cleartimeout_timeoutobject

0 commit comments

Comments
 (0)