{ "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.

\n

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.

\n" }, { "textRaw": "Class: Timeout", "type": "class", "name": "Timeout", "desc": "

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.

\n

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.

\n", "methods": [ { "textRaw": "timeout.ref()", "type": "method", "name": "ref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "desc": "

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.

\n

Note: By default, all Timeout objects are "ref'd", making it normally\nunnecessary to call timeout.ref() unless timeout.unref() had been called\npreviously.

\n

Returns a reference to the Timeout.

\n", "signatures": [ { "params": [] } ] }, { "textRaw": "timeout.unref()", "type": "method", "name": "unref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "desc": "

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.

\n

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.

\n

Returns a reference to the Timeout.

\n", "signatures": [ { "params": [] } ] } ] } ], "modules": [ { "textRaw": "Scheduling Timers", "name": "scheduling_timers", "desc": "

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" ], "changes": [] }, "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().

\n

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.

\n

If callback is not a function, a TypeError will be thrown.

\n" }, { "textRaw": "setInterval(callback, delay[, ...args])", "type": "method", "name": "setInterval", "meta": { "added": [ "v0.0.1" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`callback` {Function} The function to call when the timer elapses. ", "name": "callback", "type": "Function", "desc": "The function to call when the timer elapses." }, { "textRaw": "`delay` {number} The number of milliseconds to wait before calling the `callback`. ", "name": "delay", "type": "number", "desc": "The number of milliseconds to wait before calling the `callback`." }, { "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": "delay" }, { "name": "...args", "optional": true } ] } ], "desc": "

Schedules repeated execution of callback every delay milliseconds.\nReturns a Timeout for use with clearInterval().

\n

When delay is larger than 2147483647 or less than 1, the delay will be\nset to 1.

\n

If callback is not a function, a TypeError will be thrown.

\n" }, { "textRaw": "setTimeout(callback, delay[, ...args])", "type": "method", "name": "setTimeout", "meta": { "added": [ "v0.0.1" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`callback` {Function} The function to call when the timer elapses. ", "name": "callback", "type": "Function", "desc": "The function to call when the timer elapses." }, { "textRaw": "`delay` {number} The number of milliseconds to wait before calling the `callback`. ", "name": "delay", "type": "number", "desc": "The number of milliseconds to wait before calling the `callback`." }, { "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": "delay" }, { "name": "...args", "optional": true } ] } ], "desc": "

Schedules execution of a one-time callback after delay milliseconds.\nReturns a Timeout for use with clearTimeout().

\n

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.

\n

Note: When delay is larger than 2147483647 or less than 1, the delay\nwill be set to 1.

\n

If callback is not a function, a TypeError will be thrown.

\n" } ], "type": "module", "displayName": "Scheduling Timers" }, { "textRaw": "Cancelling Timers", "name": "cancelling_timers", "desc": "

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.

\n", "methods": [ { "textRaw": "clearImmediate(immediate)", "type": "method", "name": "clearImmediate", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`immediate` {Immediate} An `Immediate` object as returned by [`setImmediate()`][]. ", "name": "immediate", "type": "Immediate", "desc": "An `Immediate` object as returned by [`setImmediate()`][]." } ] }, { "params": [ { "name": "immediate" } ] } ], "desc": "

Cancels an Immediate object created by setImmediate().

\n" }, { "textRaw": "clearInterval(timeout)", "type": "method", "name": "clearInterval", "meta": { "added": [ "v0.0.1" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`timeout` {Timeout} A `Timeout` object as returned by [`setInterval()`][]. ", "name": "timeout", "type": "Timeout", "desc": "A `Timeout` object as returned by [`setInterval()`][]." } ] }, { "params": [ { "name": "timeout" } ] } ], "desc": "

Cancels a Timeout object created by setInterval().

\n" }, { "textRaw": "clearTimeout(timeout)", "type": "method", "name": "clearTimeout", "meta": { "added": [ "v0.0.1" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`timeout` {Timeout} A `Timeout` object as returned by [`setTimeout()`][]. ", "name": "timeout", "type": "Timeout", "desc": "A `Timeout` object as returned by [`setTimeout()`][]." } ] }, { "params": [ { "name": "timeout" } ] } ], "desc": "

Cancels a Timeout object created by setTimeout().

\n" } ], "type": "module", "displayName": "Cancelling Timers" } ], "type": "module", "displayName": "Timers" } ] }