{ "source": "doc/api/timers.md", "modules": [ { "textRaw": "Timers", "name": "timers", "stability": 3, "stabilityText": "Locked", "desc": "
The timer
module exposes a global API for scheduling functions to\nbe called at some future period of time. Because the timer functions are\nglobals, there is no need to call require('timers')
to use the API.
The timer functions within Node.js implement a similar API as the timers API\nprovided by Web Browsers but use a different internal implementation that is\nbuilt around the Node.js Event Loop.
\n", "classes": [ { "textRaw": "Class: Immediate", "type": "class", "name": "Immediate", "desc": "This object is created internally and is returned from setImmediate()
. It\ncan be passed to clearImmediate()
in order to cancel the scheduled\nactions.
This object is created internally and is returned from setTimeout()
and\nsetInterval()
. It can be passed to clearTimeout()
or\nclearInterval()
(respectively) in order to cancel the scheduled actions.
By default, when a timer is scheduled using either setTimeout()
or\nsetInterval()
, the Node.js event loop will continue running as long as the\ntimer is active. Each of the Timeout
objects returned by these functions\nexport both timeout.ref()
and timeout.unref()
functions that can be used to\ncontrol this default behavior.
When called, requests that the Node.js event loop not exit so long as the\nTimeout
is active. Calling timeout.ref()
multiple times will have no effect.
Note: By default, all Timeout
objects are "ref'd", making it normally\nunnecessary to call timeout.ref()
unless timeout.unref()
had been called\npreviously.
Returns a reference to the Timeout
.
When called, the active Timeout
object will not require the Node.js event loop\nto remain active. If there is no other activity keeping the event loop running,\nthe process may exit before the Timeout
object's callback is invoked. Calling\ntimeout.unref()
multiple times will have no effect.
Note: Calling timeout.unref()
creates an internal timer that will wake the\nNode.js event loop. Creating too many of these can adversely impact performance\nof the Node.js application.
Returns a reference to the Timeout
.
A timer in Node.js is an internal construct that calls a given function after\na certain period of time. When a timer's function is called varies depending on\nwhich method was used to create the timer and what other work the Node.js\nevent loop is doing.
\n", "methods": [ { "textRaw": "setImmediate(callback[, ...args])", "type": "method", "name": "setImmediate", "meta": { "added": [ "v0.9.1" ] }, "signatures": [ { "params": [ { "textRaw": "`callback` {Function} The function to call at the end of this turn of [the Node.js Event Loop] ", "name": "callback", "type": "Function", "desc": "The function to call at the end of this turn of [the Node.js Event Loop]" }, { "textRaw": "`...args` {any} Optional arguments to pass when the `callback` is called. ", "name": "...args", "type": "any", "desc": "Optional arguments to pass when the `callback` is called.", "optional": true } ] }, { "params": [ { "name": "callback" }, { "name": "...args", "optional": true } ] } ], "desc": "Schedules the "immediate" execution of the callback
after I/O events'\ncallbacks and before timers created using setTimeout()
and\nsetInterval()
are triggered. Returns an Immediate
for use with\nclearImmediate()
.
When multiple calls to setImmediate()
are made, the callback
functions are\nqueued for execution in the order in which they are created. The entire callback\nqueue is processed every event loop iteration. If an immediate timer is queued\nfrom inside an executing callback, that timer will not be triggered until the\nnext event loop iteration.
If callback
is not a function, a TypeError
will be thrown.
Schedules repeated execution of callback
every delay
milliseconds.\nReturns a Timeout
for use with clearInterval()
.
When delay
is larger than 2147483647
or less than 1
, the delay
will be\nset to 1
.
If callback
is not a function, a TypeError
will be thrown.
Schedules execution of a one-time callback
after delay
milliseconds.\nReturns a Timeout
for use with clearTimeout()
.
The callback
will likely not be invoked in precisely delay
milliseconds.\nNode.js makes no guarantees about the exact timing of when callbacks will fire,\nnor of their ordering. The callback will be called as close as possible to the\ntime specified.
Note: When delay
is larger than 2147483647
or less than 1
, the delay
\nwill be set to 1
.
If callback
is not a function, a TypeError
will be thrown.
The setImmediate()
, setInterval()
, and setTimeout()
methods\neach return objects that represent the scheduled timers. These can be used to\ncancel the timer and prevent it from triggering.
Cancels an Immediate
object created by setImmediate()
.
Cancels a Timeout
object created by setInterval()
.
Cancels a Timeout
object created by setTimeout()
.