11cb0ef41Sopenharmony_ci# Timers 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci> Stability: 2 - Stable 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- source_link=lib/timers.js --> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciThe `timer` module exposes a global API for scheduling functions to 101cb0ef41Sopenharmony_cibe called at some future period of time. Because the timer functions are 111cb0ef41Sopenharmony_ciglobals, there is no need to call `require('node:timers')` to use the API. 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciThe timer functions within Node.js implement a similar API as the timers API 141cb0ef41Sopenharmony_ciprovided by Web Browsers but use a different internal implementation that is 151cb0ef41Sopenharmony_cibuilt around the Node.js [Event Loop][]. 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci## Class: `Immediate` 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciThis object is created internally and is returned from [`setImmediate()`][]. It 201cb0ef41Sopenharmony_cican be passed to [`clearImmediate()`][] in order to cancel the scheduled 211cb0ef41Sopenharmony_ciactions. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciBy default, when an immediate is scheduled, the Node.js event loop will continue 241cb0ef41Sopenharmony_cirunning as long as the immediate is active. The `Immediate` object returned by 251cb0ef41Sopenharmony_ci[`setImmediate()`][] exports both `immediate.ref()` and `immediate.unref()` 261cb0ef41Sopenharmony_cifunctions that can be used to control this default behavior. 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci### `immediate.hasRef()` 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci<!-- YAML 311cb0ef41Sopenharmony_ciadded: v11.0.0 321cb0ef41Sopenharmony_ci--> 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci* Returns: {boolean} 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciIf true, the `Immediate` object will keep the Node.js event loop active. 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci### `immediate.ref()` 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci<!-- YAML 411cb0ef41Sopenharmony_ciadded: v9.7.0 421cb0ef41Sopenharmony_ci--> 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci* Returns: {Immediate} a reference to `immediate` 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ciWhen called, requests that the Node.js event loop _not_ exit so long as the 471cb0ef41Sopenharmony_ci`Immediate` is active. Calling `immediate.ref()` multiple times will have no 481cb0ef41Sopenharmony_cieffect. 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ciBy default, all `Immediate` objects are "ref'ed", making it normally unnecessary 511cb0ef41Sopenharmony_cito call `immediate.ref()` unless `immediate.unref()` had been called previously. 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci### `immediate.unref()` 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci<!-- YAML 561cb0ef41Sopenharmony_ciadded: v9.7.0 571cb0ef41Sopenharmony_ci--> 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci* Returns: {Immediate} a reference to `immediate` 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ciWhen called, the active `Immediate` object will not require the Node.js event 621cb0ef41Sopenharmony_ciloop to remain active. If there is no other activity keeping the event loop 631cb0ef41Sopenharmony_cirunning, the process may exit before the `Immediate` object's callback is 641cb0ef41Sopenharmony_ciinvoked. Calling `immediate.unref()` multiple times will have no effect. 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci### `immediate[Symbol.dispose]()` 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci<!-- YAML 691cb0ef41Sopenharmony_ciadded: v18.18.0 701cb0ef41Sopenharmony_ci--> 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ciCancels the immediate. This is similar to calling `clearImmediate()`. 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci## Class: `Timeout` 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ciThis object is created internally and is returned from [`setTimeout()`][] and 791cb0ef41Sopenharmony_ci[`setInterval()`][]. It can be passed to either [`clearTimeout()`][] or 801cb0ef41Sopenharmony_ci[`clearInterval()`][] in order to cancel the scheduled actions. 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ciBy default, when a timer is scheduled using either [`setTimeout()`][] or 831cb0ef41Sopenharmony_ci[`setInterval()`][], the Node.js event loop will continue running as long as the 841cb0ef41Sopenharmony_citimer is active. Each of the `Timeout` objects returned by these functions 851cb0ef41Sopenharmony_ciexport both `timeout.ref()` and `timeout.unref()` functions that can be used to 861cb0ef41Sopenharmony_cicontrol this default behavior. 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci### `timeout.close()` 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci<!-- YAML 911cb0ef41Sopenharmony_ciadded: v0.9.1 921cb0ef41Sopenharmony_ci--> 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci> Stability: 3 - Legacy: Use [`clearTimeout()`][] instead. 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci* Returns: {Timeout} a reference to `timeout` 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciCancels the timeout. 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci### `timeout.hasRef()` 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci<!-- YAML 1031cb0ef41Sopenharmony_ciadded: v11.0.0 1041cb0ef41Sopenharmony_ci--> 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci* Returns: {boolean} 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ciIf true, the `Timeout` object will keep the Node.js event loop active. 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci### `timeout.ref()` 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci<!-- YAML 1131cb0ef41Sopenharmony_ciadded: v0.9.1 1141cb0ef41Sopenharmony_ci--> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci* Returns: {Timeout} a reference to `timeout` 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ciWhen called, requests that the Node.js event loop _not_ exit so long as the 1191cb0ef41Sopenharmony_ci`Timeout` is active. Calling `timeout.ref()` multiple times will have no effect. 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ciBy default, all `Timeout` objects are "ref'ed", making it normally unnecessary 1221cb0ef41Sopenharmony_cito call `timeout.ref()` unless `timeout.unref()` had been called previously. 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci### `timeout.refresh()` 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci<!-- YAML 1271cb0ef41Sopenharmony_ciadded: v10.2.0 1281cb0ef41Sopenharmony_ci--> 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci* Returns: {Timeout} a reference to `timeout` 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ciSets the timer's start time to the current time, and reschedules the timer to 1331cb0ef41Sopenharmony_cicall its callback at the previously specified duration adjusted to the current 1341cb0ef41Sopenharmony_citime. This is useful for refreshing a timer without allocating a new 1351cb0ef41Sopenharmony_ciJavaScript object. 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ciUsing this on a timer that has already called its callback will reactivate the 1381cb0ef41Sopenharmony_citimer. 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci### `timeout.unref()` 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci<!-- YAML 1431cb0ef41Sopenharmony_ciadded: v0.9.1 1441cb0ef41Sopenharmony_ci--> 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci* Returns: {Timeout} a reference to `timeout` 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ciWhen called, the active `Timeout` object will not require the Node.js event loop 1491cb0ef41Sopenharmony_cito remain active. If there is no other activity keeping the event loop running, 1501cb0ef41Sopenharmony_cithe process may exit before the `Timeout` object's callback is invoked. Calling 1511cb0ef41Sopenharmony_ci`timeout.unref()` multiple times will have no effect. 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ci### `timeout[Symbol.toPrimitive]()` 1541cb0ef41Sopenharmony_ci 1551cb0ef41Sopenharmony_ci<!-- YAML 1561cb0ef41Sopenharmony_ciadded: 1571cb0ef41Sopenharmony_ci - v14.9.0 1581cb0ef41Sopenharmony_ci - v12.19.0 1591cb0ef41Sopenharmony_ci--> 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci* Returns: {integer} a number that can be used to reference this `timeout` 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ciCoerce a `Timeout` to a primitive. The primitive can be used to 1641cb0ef41Sopenharmony_ciclear the `Timeout`. The primitive can only be used in the 1651cb0ef41Sopenharmony_cisame thread where the timeout was created. Therefore, to use it 1661cb0ef41Sopenharmony_ciacross [`worker_threads`][] it must first be passed to the correct 1671cb0ef41Sopenharmony_cithread. This allows enhanced compatibility with browser 1681cb0ef41Sopenharmony_ci`setTimeout()` and `setInterval()` implementations. 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci### `timeout[Symbol.dispose]()` 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ci<!-- YAML 1731cb0ef41Sopenharmony_ciadded: v18.18.0 1741cb0ef41Sopenharmony_ci--> 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ciCancels the timeout. 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci## Scheduling timers 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ciA timer in Node.js is an internal construct that calls a given function after 1831cb0ef41Sopenharmony_cia certain period of time. When a timer's function is called varies depending on 1841cb0ef41Sopenharmony_ciwhich method was used to create the timer and what other work the Node.js 1851cb0ef41Sopenharmony_cievent loop is doing. 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci### `setImmediate(callback[, ...args])` 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci<!-- YAML 1901cb0ef41Sopenharmony_ciadded: v0.9.1 1911cb0ef41Sopenharmony_cichanges: 1921cb0ef41Sopenharmony_ci - version: v18.0.0 1931cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 1941cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 1951cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 1961cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 1971cb0ef41Sopenharmony_ci--> 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci* `callback` {Function} The function to call at the end of this turn of 2001cb0ef41Sopenharmony_ci the Node.js [Event Loop][] 2011cb0ef41Sopenharmony_ci* `...args` {any} Optional arguments to pass when the `callback` is called. 2021cb0ef41Sopenharmony_ci* Returns: {Immediate} for use with [`clearImmediate()`][] 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ciSchedules the "immediate" execution of the `callback` after I/O events' 2051cb0ef41Sopenharmony_cicallbacks. 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ciWhen multiple calls to `setImmediate()` are made, the `callback` functions are 2081cb0ef41Sopenharmony_ciqueued for execution in the order in which they are created. The entire callback 2091cb0ef41Sopenharmony_ciqueue is processed every event loop iteration. If an immediate timer is queued 2101cb0ef41Sopenharmony_cifrom inside an executing callback, that timer will not be triggered until the 2111cb0ef41Sopenharmony_cinext event loop iteration. 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ciIf `callback` is not a function, a [`TypeError`][] will be thrown. 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ciThis method has a custom variant for promises that is available using 2161cb0ef41Sopenharmony_ci[`timersPromises.setImmediate()`][]. 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci### `setInterval(callback[, delay[, ...args]])` 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ci<!-- YAML 2211cb0ef41Sopenharmony_ciadded: v0.0.1 2221cb0ef41Sopenharmony_cichanges: 2231cb0ef41Sopenharmony_ci - version: v18.0.0 2241cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 2251cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 2261cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 2271cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 2281cb0ef41Sopenharmony_ci--> 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_ci* `callback` {Function} The function to call when the timer elapses. 2311cb0ef41Sopenharmony_ci* `delay` {number} The number of milliseconds to wait before calling the 2321cb0ef41Sopenharmony_ci `callback`. **Default:** `1`. 2331cb0ef41Sopenharmony_ci* `...args` {any} Optional arguments to pass when the `callback` is called. 2341cb0ef41Sopenharmony_ci* Returns: {Timeout} for use with [`clearInterval()`][] 2351cb0ef41Sopenharmony_ci 2361cb0ef41Sopenharmony_ciSchedules repeated execution of `callback` every `delay` milliseconds. 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ciWhen `delay` is larger than `2147483647` or less than `1`, the `delay` will be 2391cb0ef41Sopenharmony_ciset to `1`. Non-integer delays are truncated to an integer. 2401cb0ef41Sopenharmony_ci 2411cb0ef41Sopenharmony_ciIf `callback` is not a function, a [`TypeError`][] will be thrown. 2421cb0ef41Sopenharmony_ci 2431cb0ef41Sopenharmony_ciThis method has a custom variant for promises that is available using 2441cb0ef41Sopenharmony_ci[`timersPromises.setInterval()`][]. 2451cb0ef41Sopenharmony_ci 2461cb0ef41Sopenharmony_ci### `setTimeout(callback[, delay[, ...args]])` 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci<!-- YAML 2491cb0ef41Sopenharmony_ciadded: v0.0.1 2501cb0ef41Sopenharmony_cichanges: 2511cb0ef41Sopenharmony_ci - version: v18.0.0 2521cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 2531cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 2541cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 2551cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 2561cb0ef41Sopenharmony_ci--> 2571cb0ef41Sopenharmony_ci 2581cb0ef41Sopenharmony_ci* `callback` {Function} The function to call when the timer elapses. 2591cb0ef41Sopenharmony_ci* `delay` {number} The number of milliseconds to wait before calling the 2601cb0ef41Sopenharmony_ci `callback`. **Default:** `1`. 2611cb0ef41Sopenharmony_ci* `...args` {any} Optional arguments to pass when the `callback` is called. 2621cb0ef41Sopenharmony_ci* Returns: {Timeout} for use with [`clearTimeout()`][] 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ciSchedules execution of a one-time `callback` after `delay` milliseconds. 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ciThe `callback` will likely not be invoked in precisely `delay` milliseconds. 2671cb0ef41Sopenharmony_ciNode.js makes no guarantees about the exact timing of when callbacks will fire, 2681cb0ef41Sopenharmony_cinor of their ordering. The callback will be called as close as possible to the 2691cb0ef41Sopenharmony_citime specified. 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ciWhen `delay` is larger than `2147483647` or less than `1`, the `delay` 2721cb0ef41Sopenharmony_ciwill be set to `1`. Non-integer delays are truncated to an integer. 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ciIf `callback` is not a function, a [`TypeError`][] will be thrown. 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ciThis method has a custom variant for promises that is available using 2771cb0ef41Sopenharmony_ci[`timersPromises.setTimeout()`][]. 2781cb0ef41Sopenharmony_ci 2791cb0ef41Sopenharmony_ci## Cancelling timers 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ciThe [`setImmediate()`][], [`setInterval()`][], and [`setTimeout()`][] methods 2821cb0ef41Sopenharmony_cieach return objects that represent the scheduled timers. These can be used to 2831cb0ef41Sopenharmony_cicancel the timer and prevent it from triggering. 2841cb0ef41Sopenharmony_ci 2851cb0ef41Sopenharmony_ciFor the promisified variants of [`setImmediate()`][] and [`setTimeout()`][], 2861cb0ef41Sopenharmony_cian [`AbortController`][] may be used to cancel the timer. When canceled, the 2871cb0ef41Sopenharmony_cireturned Promises will be rejected with an `'AbortError'`. 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ciFor `setImmediate()`: 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci```js 2921cb0ef41Sopenharmony_ciconst { setImmediate: setImmediatePromise } = require('node:timers/promises'); 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ciconst ac = new AbortController(); 2951cb0ef41Sopenharmony_ciconst signal = ac.signal; 2961cb0ef41Sopenharmony_ci 2971cb0ef41Sopenharmony_cisetImmediatePromise('foobar', { signal }) 2981cb0ef41Sopenharmony_ci .then(console.log) 2991cb0ef41Sopenharmony_ci .catch((err) => { 3001cb0ef41Sopenharmony_ci if (err.name === 'AbortError') 3011cb0ef41Sopenharmony_ci console.error('The immediate was aborted'); 3021cb0ef41Sopenharmony_ci }); 3031cb0ef41Sopenharmony_ci 3041cb0ef41Sopenharmony_ciac.abort(); 3051cb0ef41Sopenharmony_ci``` 3061cb0ef41Sopenharmony_ci 3071cb0ef41Sopenharmony_ciFor `setTimeout()`: 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ci```js 3101cb0ef41Sopenharmony_ciconst { setTimeout: setTimeoutPromise } = require('node:timers/promises'); 3111cb0ef41Sopenharmony_ci 3121cb0ef41Sopenharmony_ciconst ac = new AbortController(); 3131cb0ef41Sopenharmony_ciconst signal = ac.signal; 3141cb0ef41Sopenharmony_ci 3151cb0ef41Sopenharmony_cisetTimeoutPromise(1000, 'foobar', { signal }) 3161cb0ef41Sopenharmony_ci .then(console.log) 3171cb0ef41Sopenharmony_ci .catch((err) => { 3181cb0ef41Sopenharmony_ci if (err.name === 'AbortError') 3191cb0ef41Sopenharmony_ci console.error('The timeout was aborted'); 3201cb0ef41Sopenharmony_ci }); 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ciac.abort(); 3231cb0ef41Sopenharmony_ci``` 3241cb0ef41Sopenharmony_ci 3251cb0ef41Sopenharmony_ci### `clearImmediate(immediate)` 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ci<!-- YAML 3281cb0ef41Sopenharmony_ciadded: v0.9.1 3291cb0ef41Sopenharmony_ci--> 3301cb0ef41Sopenharmony_ci 3311cb0ef41Sopenharmony_ci* `immediate` {Immediate} An `Immediate` object as returned by 3321cb0ef41Sopenharmony_ci [`setImmediate()`][]. 3331cb0ef41Sopenharmony_ci 3341cb0ef41Sopenharmony_ciCancels an `Immediate` object created by [`setImmediate()`][]. 3351cb0ef41Sopenharmony_ci 3361cb0ef41Sopenharmony_ci### `clearInterval(timeout)` 3371cb0ef41Sopenharmony_ci 3381cb0ef41Sopenharmony_ci<!-- YAML 3391cb0ef41Sopenharmony_ciadded: v0.0.1 3401cb0ef41Sopenharmony_ci--> 3411cb0ef41Sopenharmony_ci 3421cb0ef41Sopenharmony_ci* `timeout` {Timeout|string|number} A `Timeout` object as returned by [`setInterval()`][] 3431cb0ef41Sopenharmony_ci or the [primitive][] of the `Timeout` object as a string or a number. 3441cb0ef41Sopenharmony_ci 3451cb0ef41Sopenharmony_ciCancels a `Timeout` object created by [`setInterval()`][]. 3461cb0ef41Sopenharmony_ci 3471cb0ef41Sopenharmony_ci### `clearTimeout(timeout)` 3481cb0ef41Sopenharmony_ci 3491cb0ef41Sopenharmony_ci<!-- YAML 3501cb0ef41Sopenharmony_ciadded: v0.0.1 3511cb0ef41Sopenharmony_ci--> 3521cb0ef41Sopenharmony_ci 3531cb0ef41Sopenharmony_ci* `timeout` {Timeout|string|number} A `Timeout` object as returned by [`setTimeout()`][] 3541cb0ef41Sopenharmony_ci or the [primitive][] of the `Timeout` object as a string or a number. 3551cb0ef41Sopenharmony_ci 3561cb0ef41Sopenharmony_ciCancels a `Timeout` object created by [`setTimeout()`][]. 3571cb0ef41Sopenharmony_ci 3581cb0ef41Sopenharmony_ci## Timers Promises API 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ci<!-- YAML 3611cb0ef41Sopenharmony_ciadded: v15.0.0 3621cb0ef41Sopenharmony_cichanges: 3631cb0ef41Sopenharmony_ci - version: v16.0.0 3641cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/38112 3651cb0ef41Sopenharmony_ci description: Graduated from experimental. 3661cb0ef41Sopenharmony_ci--> 3671cb0ef41Sopenharmony_ci 3681cb0ef41Sopenharmony_ciThe `timers/promises` API provides an alternative set of timer functions 3691cb0ef41Sopenharmony_cithat return `Promise` objects. The API is accessible via 3701cb0ef41Sopenharmony_ci`require('node:timers/promises')`. 3711cb0ef41Sopenharmony_ci 3721cb0ef41Sopenharmony_ci```mjs 3731cb0ef41Sopenharmony_ciimport { 3741cb0ef41Sopenharmony_ci setTimeout, 3751cb0ef41Sopenharmony_ci setImmediate, 3761cb0ef41Sopenharmony_ci setInterval, 3771cb0ef41Sopenharmony_ci} from 'timers/promises'; 3781cb0ef41Sopenharmony_ci``` 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci```cjs 3811cb0ef41Sopenharmony_ciconst { 3821cb0ef41Sopenharmony_ci setTimeout, 3831cb0ef41Sopenharmony_ci setImmediate, 3841cb0ef41Sopenharmony_ci setInterval, 3851cb0ef41Sopenharmony_ci} = require('node:timers/promises'); 3861cb0ef41Sopenharmony_ci``` 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci### `timersPromises.setTimeout([delay[, value[, options]]])` 3891cb0ef41Sopenharmony_ci 3901cb0ef41Sopenharmony_ci<!-- YAML 3911cb0ef41Sopenharmony_ciadded: v15.0.0 3921cb0ef41Sopenharmony_ci--> 3931cb0ef41Sopenharmony_ci 3941cb0ef41Sopenharmony_ci* `delay` {number} The number of milliseconds to wait before fulfilling the 3951cb0ef41Sopenharmony_ci promise. **Default:** `1`. 3961cb0ef41Sopenharmony_ci* `value` {any} A value with which the promise is fulfilled. 3971cb0ef41Sopenharmony_ci* `options` {Object} 3981cb0ef41Sopenharmony_ci * `ref` {boolean} Set to `false` to indicate that the scheduled `Timeout` 3991cb0ef41Sopenharmony_ci should not require the Node.js event loop to remain active. 4001cb0ef41Sopenharmony_ci **Default:** `true`. 4011cb0ef41Sopenharmony_ci * `signal` {AbortSignal} An optional `AbortSignal` that can be used to 4021cb0ef41Sopenharmony_ci cancel the scheduled `Timeout`. 4031cb0ef41Sopenharmony_ci 4041cb0ef41Sopenharmony_ci```mjs 4051cb0ef41Sopenharmony_ciimport { 4061cb0ef41Sopenharmony_ci setTimeout, 4071cb0ef41Sopenharmony_ci} from 'timers/promises'; 4081cb0ef41Sopenharmony_ci 4091cb0ef41Sopenharmony_ciconst res = await setTimeout(100, 'result'); 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ciconsole.log(res); // Prints 'result' 4121cb0ef41Sopenharmony_ci``` 4131cb0ef41Sopenharmony_ci 4141cb0ef41Sopenharmony_ci```cjs 4151cb0ef41Sopenharmony_ciconst { 4161cb0ef41Sopenharmony_ci setTimeout, 4171cb0ef41Sopenharmony_ci} = require('node:timers/promises'); 4181cb0ef41Sopenharmony_ci 4191cb0ef41Sopenharmony_cisetTimeout(100, 'result').then((res) => { 4201cb0ef41Sopenharmony_ci console.log(res); // Prints 'result' 4211cb0ef41Sopenharmony_ci}); 4221cb0ef41Sopenharmony_ci``` 4231cb0ef41Sopenharmony_ci 4241cb0ef41Sopenharmony_ci### `timersPromises.setImmediate([value[, options]])` 4251cb0ef41Sopenharmony_ci 4261cb0ef41Sopenharmony_ci<!-- YAML 4271cb0ef41Sopenharmony_ciadded: v15.0.0 4281cb0ef41Sopenharmony_ci--> 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci* `value` {any} A value with which the promise is fulfilled. 4311cb0ef41Sopenharmony_ci* `options` {Object} 4321cb0ef41Sopenharmony_ci * `ref` {boolean} Set to `false` to indicate that the scheduled `Immediate` 4331cb0ef41Sopenharmony_ci should not require the Node.js event loop to remain active. 4341cb0ef41Sopenharmony_ci **Default:** `true`. 4351cb0ef41Sopenharmony_ci * `signal` {AbortSignal} An optional `AbortSignal` that can be used to 4361cb0ef41Sopenharmony_ci cancel the scheduled `Immediate`. 4371cb0ef41Sopenharmony_ci 4381cb0ef41Sopenharmony_ci```mjs 4391cb0ef41Sopenharmony_ciimport { 4401cb0ef41Sopenharmony_ci setImmediate, 4411cb0ef41Sopenharmony_ci} from 'timers/promises'; 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ciconst res = await setImmediate('result'); 4441cb0ef41Sopenharmony_ci 4451cb0ef41Sopenharmony_ciconsole.log(res); // Prints 'result' 4461cb0ef41Sopenharmony_ci``` 4471cb0ef41Sopenharmony_ci 4481cb0ef41Sopenharmony_ci```cjs 4491cb0ef41Sopenharmony_ciconst { 4501cb0ef41Sopenharmony_ci setImmediate, 4511cb0ef41Sopenharmony_ci} = require('node:timers/promises'); 4521cb0ef41Sopenharmony_ci 4531cb0ef41Sopenharmony_cisetImmediate('result').then((res) => { 4541cb0ef41Sopenharmony_ci console.log(res); // Prints 'result' 4551cb0ef41Sopenharmony_ci}); 4561cb0ef41Sopenharmony_ci``` 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ci### `timersPromises.setInterval([delay[, value[, options]]])` 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci<!-- YAML 4611cb0ef41Sopenharmony_ciadded: v15.9.0 4621cb0ef41Sopenharmony_ci--> 4631cb0ef41Sopenharmony_ci 4641cb0ef41Sopenharmony_ciReturns an async iterator that generates values in an interval of `delay` ms. 4651cb0ef41Sopenharmony_ciIf `ref` is `true`, you need to call `next()` of async iterator explicitly 4661cb0ef41Sopenharmony_cior implicitly to keep the event loop alive. 4671cb0ef41Sopenharmony_ci 4681cb0ef41Sopenharmony_ci* `delay` {number} The number of milliseconds to wait between iterations. 4691cb0ef41Sopenharmony_ci **Default:** `1`. 4701cb0ef41Sopenharmony_ci* `value` {any} A value with which the iterator returns. 4711cb0ef41Sopenharmony_ci* `options` {Object} 4721cb0ef41Sopenharmony_ci * `ref` {boolean} Set to `false` to indicate that the scheduled `Timeout` 4731cb0ef41Sopenharmony_ci between iterations should not require the Node.js event loop to 4741cb0ef41Sopenharmony_ci remain active. 4751cb0ef41Sopenharmony_ci **Default:** `true`. 4761cb0ef41Sopenharmony_ci * `signal` {AbortSignal} An optional `AbortSignal` that can be used to 4771cb0ef41Sopenharmony_ci cancel the scheduled `Timeout` between operations. 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci```mjs 4801cb0ef41Sopenharmony_ciimport { 4811cb0ef41Sopenharmony_ci setInterval, 4821cb0ef41Sopenharmony_ci} from 'timers/promises'; 4831cb0ef41Sopenharmony_ci 4841cb0ef41Sopenharmony_ciconst interval = 100; 4851cb0ef41Sopenharmony_cifor await (const startTime of setInterval(interval, Date.now())) { 4861cb0ef41Sopenharmony_ci const now = Date.now(); 4871cb0ef41Sopenharmony_ci console.log(now); 4881cb0ef41Sopenharmony_ci if ((now - startTime) > 1000) 4891cb0ef41Sopenharmony_ci break; 4901cb0ef41Sopenharmony_ci} 4911cb0ef41Sopenharmony_ciconsole.log(Date.now()); 4921cb0ef41Sopenharmony_ci``` 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci```cjs 4951cb0ef41Sopenharmony_ciconst { 4961cb0ef41Sopenharmony_ci setInterval, 4971cb0ef41Sopenharmony_ci} = require('node:timers/promises'); 4981cb0ef41Sopenharmony_ciconst interval = 100; 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ci(async function() { 5011cb0ef41Sopenharmony_ci for await (const startTime of setInterval(interval, Date.now())) { 5021cb0ef41Sopenharmony_ci const now = Date.now(); 5031cb0ef41Sopenharmony_ci console.log(now); 5041cb0ef41Sopenharmony_ci if ((now - startTime) > 1000) 5051cb0ef41Sopenharmony_ci break; 5061cb0ef41Sopenharmony_ci } 5071cb0ef41Sopenharmony_ci console.log(Date.now()); 5081cb0ef41Sopenharmony_ci})(); 5091cb0ef41Sopenharmony_ci``` 5101cb0ef41Sopenharmony_ci 5111cb0ef41Sopenharmony_ci### `timersPromises.scheduler.wait(delay[, options])` 5121cb0ef41Sopenharmony_ci 5131cb0ef41Sopenharmony_ci<!-- YAML 5141cb0ef41Sopenharmony_ciadded: 5151cb0ef41Sopenharmony_ci - v17.3.0 5161cb0ef41Sopenharmony_ci - v16.14.0 5171cb0ef41Sopenharmony_ci--> 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 5201cb0ef41Sopenharmony_ci 5211cb0ef41Sopenharmony_ci* `delay` {number} The number of milliseconds to wait before resolving the 5221cb0ef41Sopenharmony_ci promise. 5231cb0ef41Sopenharmony_ci* `options` {Object} 5241cb0ef41Sopenharmony_ci * `signal` {AbortSignal} An optional `AbortSignal` that can be used to 5251cb0ef41Sopenharmony_ci cancel waiting. 5261cb0ef41Sopenharmony_ci* Returns: {Promise} 5271cb0ef41Sopenharmony_ci 5281cb0ef41Sopenharmony_ciAn experimental API defined by the [Scheduling APIs][] draft specification 5291cb0ef41Sopenharmony_cibeing developed as a standard Web Platform API. 5301cb0ef41Sopenharmony_ci 5311cb0ef41Sopenharmony_ciCalling `timersPromises.scheduler.wait(delay, options)` is roughly equivalent 5321cb0ef41Sopenharmony_cito calling `timersPromises.setTimeout(delay, undefined, options)` except that 5331cb0ef41Sopenharmony_cithe `ref` option is not supported. 5341cb0ef41Sopenharmony_ci 5351cb0ef41Sopenharmony_ci```mjs 5361cb0ef41Sopenharmony_ciimport { scheduler } from 'node:timers/promises'; 5371cb0ef41Sopenharmony_ci 5381cb0ef41Sopenharmony_ciawait scheduler.wait(1000); // Wait one second before continuing 5391cb0ef41Sopenharmony_ci``` 5401cb0ef41Sopenharmony_ci 5411cb0ef41Sopenharmony_ci### `timersPromises.scheduler.yield()` 5421cb0ef41Sopenharmony_ci 5431cb0ef41Sopenharmony_ci<!-- YAML 5441cb0ef41Sopenharmony_ciadded: 5451cb0ef41Sopenharmony_ci - v17.3.0 5461cb0ef41Sopenharmony_ci - v16.14.0 5471cb0ef41Sopenharmony_ci--> 5481cb0ef41Sopenharmony_ci 5491cb0ef41Sopenharmony_ci> Stability: 1 - Experimental 5501cb0ef41Sopenharmony_ci 5511cb0ef41Sopenharmony_ci* Returns: {Promise} 5521cb0ef41Sopenharmony_ci 5531cb0ef41Sopenharmony_ciAn experimental API defined by the [Scheduling APIs][] draft specification 5541cb0ef41Sopenharmony_cibeing developed as a standard Web Platform API. 5551cb0ef41Sopenharmony_ci 5561cb0ef41Sopenharmony_ciCalling `timersPromises.scheduler.yield()` is equivalent to calling 5571cb0ef41Sopenharmony_ci`timersPromises.setImmediate()` with no arguments. 5581cb0ef41Sopenharmony_ci 5591cb0ef41Sopenharmony_ci[Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout 5601cb0ef41Sopenharmony_ci[Scheduling APIs]: https://github.com/WICG/scheduling-apis 5611cb0ef41Sopenharmony_ci[`AbortController`]: globals.md#class-abortcontroller 5621cb0ef41Sopenharmony_ci[`TypeError`]: errors.md#class-typeerror 5631cb0ef41Sopenharmony_ci[`clearImmediate()`]: #clearimmediateimmediate 5641cb0ef41Sopenharmony_ci[`clearInterval()`]: #clearintervaltimeout 5651cb0ef41Sopenharmony_ci[`clearTimeout()`]: #cleartimeouttimeout 5661cb0ef41Sopenharmony_ci[`setImmediate()`]: #setimmediatecallback-args 5671cb0ef41Sopenharmony_ci[`setInterval()`]: #setintervalcallback-delay-args 5681cb0ef41Sopenharmony_ci[`setTimeout()`]: #settimeoutcallback-delay-args 5691cb0ef41Sopenharmony_ci[`timersPromises.setImmediate()`]: #timerspromisessetimmediatevalue-options 5701cb0ef41Sopenharmony_ci[`timersPromises.setInterval()`]: #timerspromisessetintervaldelay-value-options 5711cb0ef41Sopenharmony_ci[`timersPromises.setTimeout()`]: #timerspromisessettimeoutdelay-value-options 5721cb0ef41Sopenharmony_ci[`worker_threads`]: worker_threads.md 5731cb0ef41Sopenharmony_ci[primitive]: #timeoutsymboltoprimitive 574