nodejs-mozilla/doc/api/timers.json

428 lines
23 KiB
JSON

{
"type": "module",
"source": "doc/api/timers.md",
"modules": [
{
"textRaw": "Timers",
"name": "timers",
"introduced_in": "v0.10.0",
"stability": 2,
"stabilityText": "Stable",
"desc": "<p><strong>Source Code:</strong> <a href=\"https://github.com/nodejs/node/blob/v12.22.12/lib/timers.js\">lib/timers.js</a></p>\n<p>The <code>timer</code> 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 <code>require('timers')</code> to use the API.</p>\n<p>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 <a href=\"https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout\">Event Loop</a>.</p>",
"classes": [
{
"textRaw": "Class: `Immediate`",
"type": "class",
"name": "Immediate",
"desc": "<p>This object is created internally and is returned from <a href=\"timers.html#timers_setimmediate_callback_args\"><code>setImmediate()</code></a>. It\ncan be passed to <a href=\"timers.html#timers_clearimmediate_immediate\"><code>clearImmediate()</code></a> in order to cancel the scheduled\nactions.</p>\n<p>By default, when an immediate is scheduled, the Node.js event loop will continue\nrunning as long as the immediate is active. The <code>Immediate</code> object returned by\n<a href=\"timers.html#timers_setimmediate_callback_args\"><code>setImmediate()</code></a> exports both <code>immediate.ref()</code> and <code>immediate.unref()</code>\nfunctions that can be used to control this default behavior.</p>",
"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": "<p>If true, the <code>Immediate</code> object will keep the Node.js event loop active.</p>"
},
{
"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": "<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the\n<code>Immediate</code> is active. Calling <code>immediate.ref()</code> multiple times will have no\neffect.</p>\n<p>By default, all <code>Immediate</code> objects are \"ref'ed\", making it normally unnecessary\nto call <code>immediate.ref()</code> unless <code>immediate.unref()</code> had been called previously.</p>"
},
{
"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": "<p>When called, the active <code>Immediate</code> 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 <code>Immediate</code> object's callback is\ninvoked. Calling <code>immediate.unref()</code> multiple times will have no effect.</p>"
}
]
},
{
"textRaw": "Class: `Timeout`",
"type": "class",
"name": "Timeout",
"desc": "<p>This object is created internally and is returned from <a href=\"timers.html#timers_settimeout_callback_delay_args\"><code>setTimeout()</code></a> and\n<a href=\"timers.html#timers_setinterval_callback_delay_args\"><code>setInterval()</code></a>. It can be passed to either <a href=\"timers.html#timers_cleartimeout_timeout\"><code>clearTimeout()</code></a> or\n<a href=\"timers.html#timers_clearinterval_timeout\"><code>clearInterval()</code></a> in order to cancel the scheduled actions.</p>\n<p>By default, when a timer is scheduled using either <a href=\"timers.html#timers_settimeout_callback_delay_args\"><code>setTimeout()</code></a> or\n<a href=\"timers.html#timers_setinterval_callback_delay_args\"><code>setInterval()</code></a>, the Node.js event loop will continue running as long as the\ntimer is active. Each of the <code>Timeout</code> objects returned by these functions\nexport both <code>timeout.ref()</code> and <code>timeout.unref()</code> functions that can be used to\ncontrol this default behavior.</p>",
"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": "<p>If true, the <code>Timeout</code> object will keep the Node.js event loop active.</p>"
},
{
"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": "<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the\n<code>Timeout</code> is active. Calling <code>timeout.ref()</code> multiple times will have no effect.</p>\n<p>By default, all <code>Timeout</code> objects are \"ref'ed\", making it normally unnecessary\nto call <code>timeout.ref()</code> unless <code>timeout.unref()</code> had been called previously.</p>"
},
{
"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": "<p>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.</p>\n<p>Using this on a timer that has already called its callback will reactivate the\ntimer.</p>"
},
{
"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": "<p>When called, the active <code>Timeout</code> 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 <code>Timeout</code> object's callback is invoked. Calling\n<code>timeout.unref()</code> multiple times will have no effect.</p>\n<p>Calling <code>timeout.unref()</code> 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.</p>"
},
{
"textRaw": "`timeout[Symbol.toPrimitive]()`",
"type": "method",
"name": "[Symbol.toPrimitive]",
"meta": {
"added": [
"v12.19.0"
],
"changes": []
},
"signatures": [
{
"return": {
"textRaw": "Returns: {integer} a number that can be used to reference this `timeout`",
"name": "return",
"type": "integer",
"desc": "a number that can be used to reference this `timeout`"
},
"params": []
}
],
"desc": "<p>Coerce a <code>Timeout</code> to a primitive. The primitive can be used to\nclear the <code>Timeout</code>. The primitive can only be used in the\nsame thread where the timeout was created. Therefore, to use it\nacross <a href=\"worker_threads.html\"><code>worker_threads</code></a> it must first be passed to the correct\nthread. This allows enhanced compatibility with browser\n<code>setTimeout()</code> and <code>setInterval()</code> implementations.</p>"
}
]
}
],
"modules": [
{
"textRaw": "Scheduling timers",
"name": "scheduling_timers",
"desc": "<p>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.</p>",
"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": "<p>Schedules the \"immediate\" execution of the <code>callback</code> after I/O events'\ncallbacks.</p>\n<p>When multiple calls to <code>setImmediate()</code> are made, the <code>callback</code> 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.</p>\n<p>If <code>callback</code> is not a function, a <a href=\"errors.html#errors_class_typeerror\"><code>TypeError</code></a> will be thrown.</p>\n<p>This method has a custom variant for promises that is available using\n<a href=\"util.html#util_util_promisify_original\"><code>util.promisify()</code></a>:</p>\n<pre><code class=\"language-js\">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</code></pre>"
},
{
"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": "<p>Schedules repeated execution of <code>callback</code> every <code>delay</code> milliseconds.</p>\n<p>When <code>delay</code> is larger than <code>2147483647</code> or less than <code>1</code>, the <code>delay</code> will be\nset to <code>1</code>. Non-integer delays are truncated to an integer.</p>\n<p>If <code>callback</code> is not a function, a <a href=\"errors.html#errors_class_typeerror\"><code>TypeError</code></a> will be thrown.</p>"
},
{
"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": "<p>Schedules execution of a one-time <code>callback</code> after <code>delay</code> milliseconds.</p>\n<p>The <code>callback</code> will likely not be invoked in precisely <code>delay</code> 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.</p>\n<p>When <code>delay</code> is larger than <code>2147483647</code> or less than <code>1</code>, the <code>delay</code>\nwill be set to <code>1</code>. Non-integer delays are truncated to an integer.</p>\n<p>If <code>callback</code> is not a function, a <a href=\"errors.html#errors_class_typeerror\"><code>TypeError</code></a> will be thrown.</p>\n<p>This method has a custom variant for promises that is available using\n<a href=\"util.html#util_util_promisify_original\"><code>util.promisify()</code></a>:</p>\n<pre><code class=\"language-js\">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</code></pre>"
}
],
"type": "module",
"displayName": "Scheduling timers"
},
{
"textRaw": "Cancelling timers",
"name": "cancelling_timers",
"desc": "<p>The <a href=\"timers.html#timers_setimmediate_callback_args\"><code>setImmediate()</code></a>, <a href=\"timers.html#timers_setinterval_callback_delay_args\"><code>setInterval()</code></a>, and <a href=\"timers.html#timers_settimeout_callback_delay_args\"><code>setTimeout()</code></a> methods\neach return objects that represent the scheduled timers. These can be used to\ncancel the timer and prevent it from triggering.</p>\n<p>It is not possible to cancel timers that were created using the promisified\nvariants of <a href=\"timers.html#timers_setimmediate_callback_args\"><code>setImmediate()</code></a>, <a href=\"timers.html#timers_settimeout_callback_delay_args\"><code>setTimeout()</code></a>.</p>",
"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": "<p>Cancels an <code>Immediate</code> object created by <a href=\"timers.html#timers_setimmediate_callback_args\"><code>setImmediate()</code></a>.</p>"
},
{
"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": "<p>Cancels a <code>Timeout</code> object created by <a href=\"timers.html#timers_setinterval_callback_delay_args\"><code>setInterval()</code></a>.</p>"
},
{
"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": "<p>Cancels a <code>Timeout</code> object created by <a href=\"timers.html#timers_settimeout_callback_delay_args\"><code>setTimeout()</code></a>.</p>"
}
],
"type": "module",
"displayName": "Cancelling timers"
}
],
"type": "module",
"displayName": "Timers"
}
]
}