{ "type": "module", "source": "doc/api/timers.md", "modules": [ { "textRaw": "Timers", "name": "timers", "introduced_in": "v0.10.0", "stability": 2, "stabilityText": "Stable", "desc": "

Source Code: lib/timers.js

\n

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.

", "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

By default, when an immediate is scheduled, the Node.js event loop will continue\nrunning as long as the immediate is active. The Immediate object returned by\nsetImmediate() exports both immediate.ref() and immediate.unref()\nfunctions that can be used to control this default behavior.

", "methods": [ { "textRaw": "`immediate.hasRef()`", "type": "method", "name": "hasRef", "meta": { "added": [ "v11.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

If true, the Immediate object will keep the Node.js event loop active.

" }, { "textRaw": "`immediate.ref()`", "type": "method", "name": "ref", "meta": { "added": [ "v9.7.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Immediate} a reference to `immediate`", "name": "return", "type": "Immediate", "desc": "a reference to `immediate`" }, "params": [] } ], "desc": "

When called, requests that the Node.js event loop not exit so long as the\nImmediate is active. Calling immediate.ref() multiple times will have no\neffect.

\n

By default, all Immediate objects are \"ref'ed\", making it normally unnecessary\nto call immediate.ref() unless immediate.unref() had been called previously.

" }, { "textRaw": "`immediate.unref()`", "type": "method", "name": "unref", "meta": { "added": [ "v9.7.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Immediate} a reference to `immediate`", "name": "return", "type": "Immediate", "desc": "a reference to `immediate`" }, "params": [] } ], "desc": "

When called, the active Immediate object will not require the Node.js event\nloop to remain active. If there is no other activity keeping the event loop\nrunning, the process may exit before the Immediate object's callback is\ninvoked. Calling immediate.unref() multiple times will have no effect.

" } ] }, { "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 either clearTimeout() or\nclearInterval() 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.

", "methods": [ { "textRaw": "`timeout.hasRef()`", "type": "method", "name": "hasRef", "meta": { "added": [ "v11.0.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {boolean}", "name": "return", "type": "boolean" }, "params": [] } ], "desc": "

If true, the Timeout object will keep the Node.js event loop active.

" }, { "textRaw": "`timeout.ref()`", "type": "method", "name": "ref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Timeout} a reference to `timeout`", "name": "return", "type": "Timeout", "desc": "a reference to `timeout`" }, "params": [] } ], "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

By default, all Timeout objects are \"ref'ed\", making it normally unnecessary\nto call timeout.ref() unless timeout.unref() had been called previously.

" }, { "textRaw": "`timeout.refresh()`", "type": "method", "name": "refresh", "meta": { "added": [ "v10.2.0" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Timeout} a reference to `timeout`", "name": "return", "type": "Timeout", "desc": "a reference to `timeout`" }, "params": [] } ], "desc": "

Sets the timer's start time to the current time, and reschedules the timer to\ncall its callback at the previously specified duration adjusted to the current\ntime. This is useful for refreshing a timer without allocating a new\nJavaScript object.

\n

Using this on a timer that has already called its callback will reactivate the\ntimer.

" }, { "textRaw": "`timeout.unref()`", "type": "method", "name": "unref", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Timeout} a reference to `timeout`", "name": "return", "type": "Timeout", "desc": "a reference to `timeout`" }, "params": [] } ], "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

Calling timeout.unref() creates an internal timer that will wake the Node.js\nevent loop. Creating too many of these can adversely impact performance\nof the Node.js application.

" } ] } ], "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.

", "methods": [ { "textRaw": "`setImmediate(callback[, ...args])`", "type": "method", "name": "setImmediate", "meta": { "added": [ "v0.9.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Immediate} for use with [`clearImmediate()`][]", "name": "return", "type": "Immediate", "desc": "for use with [`clearImmediate()`][]" }, "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." } ] } ], "desc": "

Schedules the \"immediate\" execution of the callback after I/O events'\ncallbacks.

\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

This method has a custom variant for promises that is available using\nutil.promisify():

\n
const util = require('util');\nconst setImmediatePromise = util.promisify(setImmediate);\n\nsetImmediatePromise('foobar').then((value) => {\n  // value === 'foobar' (passing values is optional)\n  // This is executed after all I/O callbacks.\n});\n\n// Or with async function\nasync function timerExample() {\n  console.log('Before I/O callbacks');\n  await setImmediatePromise();\n  console.log('After I/O callbacks');\n}\ntimerExample();\n
" }, { "textRaw": "`setInterval(callback, delay[, ...args])`", "type": "method", "name": "setInterval", "meta": { "added": [ "v0.0.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Timeout} for use with [`clearInterval()`][]", "name": "return", "type": "Timeout", "desc": "for use with [`clearInterval()`][]" }, "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." } ] } ], "desc": "

Schedules repeated execution of callback every delay milliseconds.

\n

When delay is larger than 2147483647 or less than 1, the delay will be\nset to 1. Non-integer delays are truncated to an integer.

\n

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

" }, { "textRaw": "`setTimeout(callback, delay[, ...args])`", "type": "method", "name": "setTimeout", "meta": { "added": [ "v0.0.1" ], "changes": [] }, "signatures": [ { "return": { "textRaw": "Returns: {Timeout} for use with [`clearTimeout()`][]", "name": "return", "type": "Timeout", "desc": "for use with [`clearTimeout()`][]" }, "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." } ] } ], "desc": "

Schedules execution of a one-time callback after delay milliseconds.

\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

When delay is larger than 2147483647 or less than 1, the delay\nwill be set to 1. Non-integer delays are truncated to an integer.

\n

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

\n

This method has a custom variant for promises that is available using\nutil.promisify():

\n
const util = require('util');\nconst setTimeoutPromise = util.promisify(setTimeout);\n\nsetTimeoutPromise(40, 'foobar').then((value) => {\n  // value === 'foobar' (passing values is optional)\n  // This is executed after about 40 milliseconds.\n});\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

It is not possible to cancel timers that were created using the promisified\nvariants of setImmediate(), setTimeout().

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

Cancels an Immediate object created by setImmediate().

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

Cancels a Timeout object created by setInterval().

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

Cancels a Timeout object created by setTimeout().

" } ], "type": "module", "displayName": "Cancelling timers" } ], "type": "module", "displayName": "Timers" } ] }