xref: /third_party/node/doc/api/timers.html (revision 1cb0ef41)
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.20.1">
7  <title>Timers | Node.js v18.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/timers.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13  <style>@media(max-width:326px){.with-13-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:342px){.with-15-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:334px){.with-14-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
14</head>
15<body class="alt apidoc" id="api-section-timers">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers active">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="timers" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.20.1 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.20.1</li>
115            
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#timers">Timers</a></span>
124<ul>
125<li><a href="#class-immediate">Class: <code>Immediate</code></a>
126<ul>
127<li><a href="#immediatehasref"><code>immediate.hasRef()</code></a></li>
128<li><a href="#immediateref"><code>immediate.ref()</code></a></li>
129<li><a href="#immediateunref"><code>immediate.unref()</code></a></li>
130<li><span class="stability_1"><a href="#immediatesymboldispose"><code>immediate[Symbol.dispose]()</code></a></span></li>
131</ul>
132</li>
133<li><a href="#class-timeout">Class: <code>Timeout</code></a>
134<ul>
135<li><span class="stability_3"><a href="#timeoutclose"><code>timeout.close()</code></a></span></li>
136<li><a href="#timeouthasref"><code>timeout.hasRef()</code></a></li>
137<li><a href="#timeoutref"><code>timeout.ref()</code></a></li>
138<li><a href="#timeoutrefresh"><code>timeout.refresh()</code></a></li>
139<li><a href="#timeoutunref"><code>timeout.unref()</code></a></li>
140<li><a href="#timeoutsymboltoprimitive"><code>timeout[Symbol.toPrimitive]()</code></a></li>
141<li><span class="stability_1"><a href="#timeoutsymboldispose"><code>timeout[Symbol.dispose]()</code></a></span></li>
142</ul>
143</li>
144<li><a href="#scheduling-timers">Scheduling timers</a>
145<ul>
146<li><a href="#setimmediatecallback-args"><code>setImmediate(callback[, ...args])</code></a></li>
147<li><a href="#setintervalcallback-delay-args"><code>setInterval(callback[, delay[, ...args]])</code></a></li>
148<li><a href="#settimeoutcallback-delay-args"><code>setTimeout(callback[, delay[, ...args]])</code></a></li>
149</ul>
150</li>
151<li><a href="#cancelling-timers">Cancelling timers</a>
152<ul>
153<li><a href="#clearimmediateimmediate"><code>clearImmediate(immediate)</code></a></li>
154<li><a href="#clearintervaltimeout"><code>clearInterval(timeout)</code></a></li>
155<li><a href="#cleartimeouttimeout"><code>clearTimeout(timeout)</code></a></li>
156</ul>
157</li>
158<li><a href="#timers-promises-api">Timers Promises API</a>
159<ul>
160<li><a href="#timerspromisessettimeoutdelay-value-options"><code>timersPromises.setTimeout([delay[, value[, options]]])</code></a></li>
161<li><a href="#timerspromisessetimmediatevalue-options"><code>timersPromises.setImmediate([value[, options]])</code></a></li>
162<li><a href="#timerspromisessetintervaldelay-value-options"><code>timersPromises.setInterval([delay[, value[, options]]])</code></a></li>
163<li><span class="stability_1"><a href="#timerspromisesschedulerwaitdelay-options"><code>timersPromises.scheduler.wait(delay[, options])</code></a></span></li>
164<li><span class="stability_1"><a href="#timerspromisesscheduleryield"><code>timersPromises.scheduler.yield()</code></a></span></li>
165</ul>
166</li>
167</ul>
168</li>
169</ul></div></div>
170    </li>
171  
172            
173    <li class="picker-header">
174      <a href="#">
175        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
176        Index
177      </a>
178
179      <div class="picker"><ul>
180<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
181<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
182
183      <li>
184        <a href="index.html">Index</a>
185      </li>
186    </ul>
187  
188<hr class="line">
189<ul>
190<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
191<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
192<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
193<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
194<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
195<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
196<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
197<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
198<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
199<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
200<li><a href="console.html" class="nav-console">Console</a></li>
201<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
202<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
203<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
204<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
205<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
206<li><a href="dns.html" class="nav-dns">DNS</a></li>
207<li><a href="domain.html" class="nav-domain">Domain</a></li>
208<li><a href="errors.html" class="nav-errors">Errors</a></li>
209<li><a href="events.html" class="nav-events">Events</a></li>
210<li><a href="fs.html" class="nav-fs">File system</a></li>
211<li><a href="globals.html" class="nav-globals">Globals</a></li>
212<li><a href="http.html" class="nav-http">HTTP</a></li>
213<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
214<li><a href="https.html" class="nav-https">HTTPS</a></li>
215<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
216<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
217<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
218<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
219<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
220<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
221<li><a href="net.html" class="nav-net">Net</a></li>
222<li><a href="os.html" class="nav-os">OS</a></li>
223<li><a href="path.html" class="nav-path">Path</a></li>
224<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
225<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
226<li><a href="process.html" class="nav-process">Process</a></li>
227<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
228<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
229<li><a href="readline.html" class="nav-readline">Readline</a></li>
230<li><a href="repl.html" class="nav-repl">REPL</a></li>
231<li><a href="report.html" class="nav-report">Report</a></li>
232<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
233<li><a href="stream.html" class="nav-stream">Stream</a></li>
234<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
235<li><a href="test.html" class="nav-test">Test runner</a></li>
236<li><a href="timers.html" class="nav-timers active">Timers</a></li>
237<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
238<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
239<li><a href="tty.html" class="nav-tty">TTY</a></li>
240<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
241<li><a href="url.html" class="nav-url">URL</a></li>
242<li><a href="util.html" class="nav-util">Utilities</a></li>
243<li><a href="v8.html" class="nav-v8">V8</a></li>
244<li><a href="vm.html" class="nav-vm">VM</a></li>
245<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
246<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
247<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
248<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
249<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
250</ul>
251<hr class="line">
252<ul>
253<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
254</ul></div>
255    </li>
256  
257            
258    <li class="picker-header">
259      <a href="#">
260        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
261        Other versions
262      </a>
263      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/timers.html">21.x</a></li>
264<li><a href="https://nodejs.org/docs/latest-v20.x/api/timers.html">20.x <b>LTS</b></a></li>
265<li><a href="https://nodejs.org/docs/latest-v19.x/api/timers.html">19.x</a></li>
266<li><a href="https://nodejs.org/docs/latest-v18.x/api/timers.html">18.x <b>LTS</b></a></li>
267<li><a href="https://nodejs.org/docs/latest-v17.x/api/timers.html">17.x</a></li>
268<li><a href="https://nodejs.org/docs/latest-v16.x/api/timers.html">16.x</a></li>
269<li><a href="https://nodejs.org/docs/latest-v15.x/api/timers.html">15.x</a></li>
270<li><a href="https://nodejs.org/docs/latest-v14.x/api/timers.html">14.x</a></li>
271<li><a href="https://nodejs.org/docs/latest-v13.x/api/timers.html">13.x</a></li>
272<li><a href="https://nodejs.org/docs/latest-v12.x/api/timers.html">12.x</a></li>
273<li><a href="https://nodejs.org/docs/latest-v11.x/api/timers.html">11.x</a></li>
274<li><a href="https://nodejs.org/docs/latest-v10.x/api/timers.html">10.x</a></li>
275<li><a href="https://nodejs.org/docs/latest-v9.x/api/timers.html">9.x</a></li>
276<li><a href="https://nodejs.org/docs/latest-v8.x/api/timers.html">8.x</a></li>
277<li><a href="https://nodejs.org/docs/latest-v7.x/api/timers.html">7.x</a></li>
278<li><a href="https://nodejs.org/docs/latest-v6.x/api/timers.html">6.x</a></li>
279<li><a href="https://nodejs.org/docs/latest-v5.x/api/timers.html">5.x</a></li>
280<li><a href="https://nodejs.org/docs/latest-v4.x/api/timers.html">4.x</a></li>
281<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/timers.html">0.12.x</a></li>
282<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/timers.html">0.10.x</a></li></ol></div>
283    </li>
284  
285            <li class="picker-header">
286              <a href="#">
287                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
288                Options
289              </a>
290        
291              <div class="picker">
292                <ul>
293                  <li>
294                    <a href="all.html">View on single page</a>
295                  </li>
296                  <li>
297                    <a href="timers.json">View as JSON</a>
298                  </li>
299                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/timers.md">Edit on GitHub</a></li>    
300                </ul>
301              </div>
302            </li>
303          </ul>
304        </div>
305        <hr>
306      </header>
307
308      <details id="toc" open><summary>Table of contents</summary><ul>
309<li><span class="stability_2"><a href="#timers">Timers</a></span>
310<ul>
311<li><a href="#class-immediate">Class: <code>Immediate</code></a>
312<ul>
313<li><a href="#immediatehasref"><code>immediate.hasRef()</code></a></li>
314<li><a href="#immediateref"><code>immediate.ref()</code></a></li>
315<li><a href="#immediateunref"><code>immediate.unref()</code></a></li>
316<li><span class="stability_1"><a href="#immediatesymboldispose"><code>immediate[Symbol.dispose]()</code></a></span></li>
317</ul>
318</li>
319<li><a href="#class-timeout">Class: <code>Timeout</code></a>
320<ul>
321<li><span class="stability_3"><a href="#timeoutclose"><code>timeout.close()</code></a></span></li>
322<li><a href="#timeouthasref"><code>timeout.hasRef()</code></a></li>
323<li><a href="#timeoutref"><code>timeout.ref()</code></a></li>
324<li><a href="#timeoutrefresh"><code>timeout.refresh()</code></a></li>
325<li><a href="#timeoutunref"><code>timeout.unref()</code></a></li>
326<li><a href="#timeoutsymboltoprimitive"><code>timeout[Symbol.toPrimitive]()</code></a></li>
327<li><span class="stability_1"><a href="#timeoutsymboldispose"><code>timeout[Symbol.dispose]()</code></a></span></li>
328</ul>
329</li>
330<li><a href="#scheduling-timers">Scheduling timers</a>
331<ul>
332<li><a href="#setimmediatecallback-args"><code>setImmediate(callback[, ...args])</code></a></li>
333<li><a href="#setintervalcallback-delay-args"><code>setInterval(callback[, delay[, ...args]])</code></a></li>
334<li><a href="#settimeoutcallback-delay-args"><code>setTimeout(callback[, delay[, ...args]])</code></a></li>
335</ul>
336</li>
337<li><a href="#cancelling-timers">Cancelling timers</a>
338<ul>
339<li><a href="#clearimmediateimmediate"><code>clearImmediate(immediate)</code></a></li>
340<li><a href="#clearintervaltimeout"><code>clearInterval(timeout)</code></a></li>
341<li><a href="#cleartimeouttimeout"><code>clearTimeout(timeout)</code></a></li>
342</ul>
343</li>
344<li><a href="#timers-promises-api">Timers Promises API</a>
345<ul>
346<li><a href="#timerspromisessettimeoutdelay-value-options"><code>timersPromises.setTimeout([delay[, value[, options]]])</code></a></li>
347<li><a href="#timerspromisessetimmediatevalue-options"><code>timersPromises.setImmediate([value[, options]])</code></a></li>
348<li><a href="#timerspromisessetintervaldelay-value-options"><code>timersPromises.setInterval([delay[, value[, options]]])</code></a></li>
349<li><span class="stability_1"><a href="#timerspromisesschedulerwaitdelay-options"><code>timersPromises.scheduler.wait(delay[, options])</code></a></span></li>
350<li><span class="stability_1"><a href="#timerspromisesscheduleryield"><code>timersPromises.scheduler.yield()</code></a></span></li>
351</ul>
352</li>
353</ul>
354</li>
355</ul></details>
356
357      <div id="apicontent">
358        <h2>Timers<span><a class="mark" href="#timers" id="timers">#</a></span><a aria-hidden="true" class="legacy" id="timers_timers"></a></h2>
359
360<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
361<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/timers.js">lib/timers.js</a></p>
362<p>The <code>timer</code> module exposes a global API for scheduling functions to
363be called at some future period of time. Because the timer functions are
364globals, there is no need to call <code>require('node:timers')</code> to use the API.</p>
365<p>The timer functions within Node.js implement a similar API as the timers API
366provided by Web Browsers but use a different internal implementation that is
367built around the Node.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout">Event Loop</a>.</p>
368<section><h3>Class: <code>Immediate</code><span><a class="mark" href="#class-immediate" id="class-immediate">#</a></span><a aria-hidden="true" class="legacy" id="timers_class_immediate"></a></h3>
369<p>This object is created internally and is returned from <a href="#setimmediatecallback-args"><code>setImmediate()</code></a>. It
370can be passed to <a href="#clearimmediateimmediate"><code>clearImmediate()</code></a> in order to cancel the scheduled
371actions.</p>
372<p>By default, when an immediate is scheduled, the Node.js event loop will continue
373running as long as the immediate is active. The <code>Immediate</code> object returned by
374<a href="#setimmediatecallback-args"><code>setImmediate()</code></a> exports both <code>immediate.ref()</code> and <code>immediate.unref()</code>
375functions that can be used to control this default behavior.</p>
376<h4><code>immediate.hasRef()</code><span><a class="mark" href="#immediatehasref" id="immediatehasref">#</a></span><a aria-hidden="true" class="legacy" id="timers_immediate_hasref"></a></h4>
377<div class="api_metadata">
378<span>Added in: v11.0.0</span>
379</div>
380<ul>
381<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
382</ul>
383<p>If true, the <code>Immediate</code> object will keep the Node.js event loop active.</p>
384<h4><code>immediate.ref()</code><span><a class="mark" href="#immediateref" id="immediateref">#</a></span><a aria-hidden="true" class="legacy" id="timers_immediate_ref"></a></h4>
385<div class="api_metadata">
386<span>Added in: v9.7.0</span>
387</div>
388<ul>
389<li>Returns: <a href="timers.html#class-immediate" class="type">&#x3C;Immediate></a> a reference to <code>immediate</code></li>
390</ul>
391<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the
392<code>Immediate</code> is active. Calling <code>immediate.ref()</code> multiple times will have no
393effect.</p>
394<p>By default, all <code>Immediate</code> objects are "ref'ed", making it normally unnecessary
395to call <code>immediate.ref()</code> unless <code>immediate.unref()</code> had been called previously.</p>
396<h4><code>immediate.unref()</code><span><a class="mark" href="#immediateunref" id="immediateunref">#</a></span><a aria-hidden="true" class="legacy" id="timers_immediate_unref"></a></h4>
397<div class="api_metadata">
398<span>Added in: v9.7.0</span>
399</div>
400<ul>
401<li>Returns: <a href="timers.html#class-immediate" class="type">&#x3C;Immediate></a> a reference to <code>immediate</code></li>
402</ul>
403<p>When called, the active <code>Immediate</code> object will not require the Node.js event
404loop to remain active. If there is no other activity keeping the event loop
405running, the process may exit before the <code>Immediate</code> object's callback is
406invoked. Calling <code>immediate.unref()</code> multiple times will have no effect.</p>
407<h4><code>immediate[Symbol.dispose]()</code><span><a class="mark" href="#immediatesymboldispose" id="immediatesymboldispose">#</a></span><a aria-hidden="true" class="legacy" id="timers_immediate_symbol_dispose"></a></h4>
408<div class="api_metadata">
409<span>Added in: v18.18.0</span>
410</div>
411<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
412<p>Cancels the immediate. This is similar to calling <code>clearImmediate()</code>.</p>
413</section><section><h3>Class: <code>Timeout</code><span><a class="mark" href="#class-timeout" id="class-timeout">#</a></span><a aria-hidden="true" class="legacy" id="timers_class_timeout"></a></h3>
414<p>This object is created internally and is returned from <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a> and
415<a href="#setintervalcallback-delay-args"><code>setInterval()</code></a>. It can be passed to either <a href="#cleartimeouttimeout"><code>clearTimeout()</code></a> or
416<a href="#clearintervaltimeout"><code>clearInterval()</code></a> in order to cancel the scheduled actions.</p>
417<p>By default, when a timer is scheduled using either <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a> or
418<a href="#setintervalcallback-delay-args"><code>setInterval()</code></a>, the Node.js event loop will continue running as long as the
419timer is active. Each of the <code>Timeout</code> objects returned by these functions
420export both <code>timeout.ref()</code> and <code>timeout.unref()</code> functions that can be used to
421control this default behavior.</p>
422<h4><code>timeout.close()</code><span><a class="mark" href="#timeoutclose" id="timeoutclose">#</a></span><a aria-hidden="true" class="legacy" id="timers_timeout_close"></a></h4>
423<div class="api_metadata">
424<span>Added in: v0.9.1</span>
425</div>
426<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy: Use <a href="#cleartimeouttimeout"><code>clearTimeout()</code></a> instead.</div><p></p>
427<ul>
428<li>Returns: <a href="timers.html#class-timeout" class="type">&#x3C;Timeout></a> a reference to <code>timeout</code></li>
429</ul>
430<p>Cancels the timeout.</p>
431<h4><code>timeout.hasRef()</code><span><a class="mark" href="#timeouthasref" id="timeouthasref">#</a></span><a aria-hidden="true" class="legacy" id="timers_timeout_hasref"></a></h4>
432<div class="api_metadata">
433<span>Added in: v11.0.0</span>
434</div>
435<ul>
436<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
437</ul>
438<p>If true, the <code>Timeout</code> object will keep the Node.js event loop active.</p>
439<h4><code>timeout.ref()</code><span><a class="mark" href="#timeoutref" id="timeoutref">#</a></span><a aria-hidden="true" class="legacy" id="timers_timeout_ref"></a></h4>
440<div class="api_metadata">
441<span>Added in: v0.9.1</span>
442</div>
443<ul>
444<li>Returns: <a href="timers.html#class-timeout" class="type">&#x3C;Timeout></a> a reference to <code>timeout</code></li>
445</ul>
446<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the
447<code>Timeout</code> is active. Calling <code>timeout.ref()</code> multiple times will have no effect.</p>
448<p>By default, all <code>Timeout</code> objects are "ref'ed", making it normally unnecessary
449to call <code>timeout.ref()</code> unless <code>timeout.unref()</code> had been called previously.</p>
450<h4><code>timeout.refresh()</code><span><a class="mark" href="#timeoutrefresh" id="timeoutrefresh">#</a></span><a aria-hidden="true" class="legacy" id="timers_timeout_refresh"></a></h4>
451<div class="api_metadata">
452<span>Added in: v10.2.0</span>
453</div>
454<ul>
455<li>Returns: <a href="timers.html#class-timeout" class="type">&#x3C;Timeout></a> a reference to <code>timeout</code></li>
456</ul>
457<p>Sets the timer's start time to the current time, and reschedules the timer to
458call its callback at the previously specified duration adjusted to the current
459time. This is useful for refreshing a timer without allocating a new
460JavaScript object.</p>
461<p>Using this on a timer that has already called its callback will reactivate the
462timer.</p>
463<h4><code>timeout.unref()</code><span><a class="mark" href="#timeoutunref" id="timeoutunref">#</a></span><a aria-hidden="true" class="legacy" id="timers_timeout_unref"></a></h4>
464<div class="api_metadata">
465<span>Added in: v0.9.1</span>
466</div>
467<ul>
468<li>Returns: <a href="timers.html#class-timeout" class="type">&#x3C;Timeout></a> a reference to <code>timeout</code></li>
469</ul>
470<p>When called, the active <code>Timeout</code> object will not require the Node.js event loop
471to remain active. If there is no other activity keeping the event loop running,
472the process may exit before the <code>Timeout</code> object's callback is invoked. Calling
473<code>timeout.unref()</code> multiple times will have no effect.</p>
474<h4><code>timeout[Symbol.toPrimitive]()</code><span><a class="mark" href="#timeoutsymboltoprimitive" id="timeoutsymboltoprimitive">#</a></span><a aria-hidden="true" class="legacy" id="timers_timeout_symbol_toprimitive"></a></h4>
475<div class="api_metadata">
476<span>Added in: v14.9.0, v12.19.0</span>
477</div>
478<ul>
479<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> a number that can be used to reference this <code>timeout</code></li>
480</ul>
481<p>Coerce a <code>Timeout</code> to a primitive. The primitive can be used to
482clear the <code>Timeout</code>. The primitive can only be used in the
483same thread where the timeout was created. Therefore, to use it
484across <a href="worker_threads.html"><code>worker_threads</code></a> it must first be passed to the correct
485thread. This allows enhanced compatibility with browser
486<code>setTimeout()</code> and <code>setInterval()</code> implementations.</p>
487<h4><code>timeout[Symbol.dispose]()</code><span><a class="mark" href="#timeoutsymboldispose" id="timeoutsymboldispose">#</a></span><a aria-hidden="true" class="legacy" id="timers_timeout_symbol_dispose"></a></h4>
488<div class="api_metadata">
489<span>Added in: v18.18.0</span>
490</div>
491<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
492<p>Cancels the timeout.</p>
493</section><section><h3>Scheduling timers<span><a class="mark" href="#scheduling-timers" id="scheduling-timers">#</a></span><a aria-hidden="true" class="legacy" id="timers_scheduling_timers"></a></h3>
494<p>A timer in Node.js is an internal construct that calls a given function after
495a certain period of time. When a timer's function is called varies depending on
496which method was used to create the timer and what other work the Node.js
497event loop is doing.</p>
498<h4><code>setImmediate(callback[, ...args])</code><span><a class="mark" href="#setimmediatecallback-args" id="setimmediatecallback-args">#</a></span><a aria-hidden="true" class="legacy" id="timers_setimmediate_callback_args"></a></h4>
499<div class="api_metadata">
500<details class="changelog"><summary>History</summary>
501<table>
502<tbody><tr><th>Version</th><th>Changes</th></tr>
503<tr><td>v18.0.0</td>
504<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
505<tr><td>v0.9.1</td>
506<td><p><span>Added in: v0.9.1</span></p></td></tr>
507</tbody></table>
508</details>
509</div>
510<ul>
511<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to call at the end of this turn of
512the Node.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout">Event Loop</a></li>
513<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Optional arguments to pass when the <code>callback</code> is called.</li>
514<li>Returns: <a href="timers.html#class-immediate" class="type">&#x3C;Immediate></a> for use with <a href="#clearimmediateimmediate"><code>clearImmediate()</code></a></li>
515</ul>
516<p>Schedules the "immediate" execution of the <code>callback</code> after I/O events'
517callbacks.</p>
518<p>When multiple calls to <code>setImmediate()</code> are made, the <code>callback</code> functions are
519queued for execution in the order in which they are created. The entire callback
520queue is processed every event loop iteration. If an immediate timer is queued
521from inside an executing callback, that timer will not be triggered until the
522next event loop iteration.</p>
523<p>If <code>callback</code> is not a function, a <a href="errors.html#class-typeerror"><code>TypeError</code></a> will be thrown.</p>
524<p>This method has a custom variant for promises that is available using
525<a href="#timerspromisessetimmediatevalue-options"><code>timersPromises.setImmediate()</code></a>.</p>
526<h4><code>setInterval(callback[, delay[, ...args]])</code><span><a class="mark" href="#setintervalcallback-delay-args" id="setintervalcallback-delay-args">#</a></span><a aria-hidden="true" class="legacy" id="timers_setinterval_callback_delay_args"></a></h4>
527<div class="api_metadata">
528<details class="changelog"><summary>History</summary>
529<table>
530<tbody><tr><th>Version</th><th>Changes</th></tr>
531<tr><td>v18.0.0</td>
532<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
533<tr><td>v0.0.1</td>
534<td><p><span>Added in: v0.0.1</span></p></td></tr>
535</tbody></table>
536</details>
537</div>
538<ul>
539<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to call when the timer elapses.</li>
540<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds to wait before calling the
541<code>callback</code>. <strong>Default:</strong> <code>1</code>.</li>
542<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Optional arguments to pass when the <code>callback</code> is called.</li>
543<li>Returns: <a href="timers.html#class-timeout" class="type">&#x3C;Timeout></a> for use with <a href="#clearintervaltimeout"><code>clearInterval()</code></a></li>
544</ul>
545<p>Schedules repeated execution of <code>callback</code> every <code>delay</code> milliseconds.</p>
546<p>When <code>delay</code> is larger than <code>2147483647</code> or less than <code>1</code>, the <code>delay</code> will be
547set to <code>1</code>. Non-integer delays are truncated to an integer.</p>
548<p>If <code>callback</code> is not a function, a <a href="errors.html#class-typeerror"><code>TypeError</code></a> will be thrown.</p>
549<p>This method has a custom variant for promises that is available using
550<a href="#timerspromisessetintervaldelay-value-options"><code>timersPromises.setInterval()</code></a>.</p>
551<h4><code>setTimeout(callback[, delay[, ...args]])</code><span><a class="mark" href="#settimeoutcallback-delay-args" id="settimeoutcallback-delay-args">#</a></span><a aria-hidden="true" class="legacy" id="timers_settimeout_callback_delay_args"></a></h4>
552<div class="api_metadata">
553<details class="changelog"><summary>History</summary>
554<table>
555<tbody><tr><th>Version</th><th>Changes</th></tr>
556<tr><td>v18.0.0</td>
557<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
558<tr><td>v0.0.1</td>
559<td><p><span>Added in: v0.0.1</span></p></td></tr>
560</tbody></table>
561</details>
562</div>
563<ul>
564<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to call when the timer elapses.</li>
565<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds to wait before calling the
566<code>callback</code>. <strong>Default:</strong> <code>1</code>.</li>
567<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> Optional arguments to pass when the <code>callback</code> is called.</li>
568<li>Returns: <a href="timers.html#class-timeout" class="type">&#x3C;Timeout></a> for use with <a href="#cleartimeouttimeout"><code>clearTimeout()</code></a></li>
569</ul>
570<p>Schedules execution of a one-time <code>callback</code> after <code>delay</code> milliseconds.</p>
571<p>The <code>callback</code> will likely not be invoked in precisely <code>delay</code> milliseconds.
572Node.js makes no guarantees about the exact timing of when callbacks will fire,
573nor of their ordering. The callback will be called as close as possible to the
574time specified.</p>
575<p>When <code>delay</code> is larger than <code>2147483647</code> or less than <code>1</code>, the <code>delay</code>
576will be set to <code>1</code>. Non-integer delays are truncated to an integer.</p>
577<p>If <code>callback</code> is not a function, a <a href="errors.html#class-typeerror"><code>TypeError</code></a> will be thrown.</p>
578<p>This method has a custom variant for promises that is available using
579<a href="#timerspromisessettimeoutdelay-value-options"><code>timersPromises.setTimeout()</code></a>.</p>
580</section><section><h3>Cancelling timers<span><a class="mark" href="#cancelling-timers" id="cancelling-timers">#</a></span><a aria-hidden="true" class="legacy" id="timers_cancelling_timers"></a></h3>
581<p>The <a href="#setimmediatecallback-args"><code>setImmediate()</code></a>, <a href="#setintervalcallback-delay-args"><code>setInterval()</code></a>, and <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a> methods
582each return objects that represent the scheduled timers. These can be used to
583cancel the timer and prevent it from triggering.</p>
584<p>For the promisified variants of <a href="#setimmediatecallback-args"><code>setImmediate()</code></a> and <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a>,
585an <a href="globals.html#class-abortcontroller"><code>AbortController</code></a> may be used to cancel the timer. When canceled, the
586returned Promises will be rejected with an <code>'AbortError'</code>.</p>
587<p>For <code>setImmediate()</code>:</p>
588<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-attr">setImmediate</span>: setImmediatePromise } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
589
590<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
591<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
592
593<span class="hljs-title function_">setImmediatePromise</span>(<span class="hljs-string">'foobar'</span>, { signal })
594  .<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>)
595  .<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
596    <span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>)
597      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'The immediate was aborted'</span>);
598  });
599
600ac.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
601<p>For <code>setTimeout()</code>:</p>
602<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-attr">setTimeout</span>: setTimeoutPromise } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
603
604<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>();
605<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
606
607setTimeoutPromise(<span class="hljs-number">1000</span>, <span class="hljs-string">'foobar'</span>, { signal })
608  .<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>)
609  .<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">err</span>) =></span> {
610    <span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>)
611      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'The timeout was aborted'</span>);
612  });
613
614ac.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre>
615<h4><code>clearImmediate(immediate)</code><span><a class="mark" href="#clearimmediateimmediate" id="clearimmediateimmediate">#</a></span><a aria-hidden="true" class="legacy" id="timers_clearimmediate_immediate"></a></h4>
616<div class="api_metadata">
617<span>Added in: v0.9.1</span>
618</div>
619<ul>
620<li><code>immediate</code> <a href="timers.html#class-immediate" class="type">&#x3C;Immediate></a> An <code>Immediate</code> object as returned by
621<a href="#setimmediatecallback-args"><code>setImmediate()</code></a>.</li>
622</ul>
623<p>Cancels an <code>Immediate</code> object created by <a href="#setimmediatecallback-args"><code>setImmediate()</code></a>.</p>
624<h4><code>clearInterval(timeout)</code><span><a class="mark" href="#clearintervaltimeout" id="clearintervaltimeout">#</a></span><a aria-hidden="true" class="legacy" id="timers_clearinterval_timeout"></a></h4>
625<div class="api_metadata">
626<span>Added in: v0.0.1</span>
627</div>
628<ul>
629<li><code>timeout</code> <a href="timers.html#class-timeout" class="type">&#x3C;Timeout></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> A <code>Timeout</code> object as returned by <a href="#setintervalcallback-delay-args"><code>setInterval()</code></a>
630or the <a href="#timeoutsymboltoprimitive">primitive</a> of the <code>Timeout</code> object as a string or a number.</li>
631</ul>
632<p>Cancels a <code>Timeout</code> object created by <a href="#setintervalcallback-delay-args"><code>setInterval()</code></a>.</p>
633<h4><code>clearTimeout(timeout)</code><span><a class="mark" href="#cleartimeouttimeout" id="cleartimeouttimeout">#</a></span><a aria-hidden="true" class="legacy" id="timers_cleartimeout_timeout"></a></h4>
634<div class="api_metadata">
635<span>Added in: v0.0.1</span>
636</div>
637<ul>
638<li><code>timeout</code> <a href="timers.html#class-timeout" class="type">&#x3C;Timeout></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> A <code>Timeout</code> object as returned by <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a>
639or the <a href="#timeoutsymboltoprimitive">primitive</a> of the <code>Timeout</code> object as a string or a number.</li>
640</ul>
641<p>Cancels a <code>Timeout</code> object created by <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a>.</p>
642</section><section><h3>Timers Promises API<span><a class="mark" href="#timers-promises-api" id="timers-promises-api">#</a></span><a aria-hidden="true" class="legacy" id="timers_timers_promises_api"></a></h3>
643<div class="api_metadata">
644<details class="changelog"><summary>History</summary>
645<table>
646<tbody><tr><th>Version</th><th>Changes</th></tr>
647<tr><td>v16.0.0</td>
648<td><p>Graduated from experimental.</p></td></tr>
649<tr><td>v15.0.0</td>
650<td><p><span>Added in: v15.0.0</span></p></td></tr>
651</tbody></table>
652</details>
653</div>
654<p>The <code>timers/promises</code> API provides an alternative set of timer functions
655that return <code>Promise</code> objects. The API is accessible via
656<code>require('node:timers/promises')</code>.</p>
657
658<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
659  <span class="hljs-built_in">setTimeout</span>,
660  setImmediate,
661  <span class="hljs-built_in">setInterval</span>,
662} <span class="hljs-keyword">from</span> <span class="hljs-string">'timers/promises'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> {
663  <span class="hljs-built_in">setTimeout</span>,
664  setImmediate,
665  <span class="hljs-built_in">setInterval</span>,
666} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);</code><button class="copy-button">copy</button></pre>
667<h4><code>timersPromises.setTimeout([delay[, value[, options]]])</code><span><a class="mark" href="#timerspromisessettimeoutdelay-value-options" id="timerspromisessettimeoutdelay-value-options">#</a></span><a aria-hidden="true" class="legacy" id="timers_timerspromises_settimeout_delay_value_options"></a></h4>
668<div class="api_metadata">
669<span>Added in: v15.0.0</span>
670</div>
671<ul>
672<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds to wait before fulfilling the
673promise. <strong>Default:</strong> <code>1</code>.</li>
674<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> A value with which the promise is fulfilled.</li>
675<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
676<ul>
677<li><code>ref</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Set to <code>false</code> to indicate that the scheduled <code>Timeout</code>
678should not require the Node.js event loop to remain active.
679<strong>Default:</strong> <code>true</code>.</li>
680<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An optional <code>AbortSignal</code> that can be used to
681cancel the scheduled <code>Timeout</code>.</li>
682</ul>
683</li>
684</ul>
685
686<pre class="with-13-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
687  <span class="hljs-built_in">setTimeout</span>,
688} <span class="hljs-keyword">from</span> <span class="hljs-string">'timers/promises'</span>;
689
690<span class="hljs-keyword">const</span> res = <span class="hljs-keyword">await</span> <span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">100</span>, <span class="hljs-string">'result'</span>);
691
692<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res);  <span class="hljs-comment">// Prints 'result'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> {
693  <span class="hljs-built_in">setTimeout</span>,
694} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
695
696<span class="hljs-built_in">setTimeout</span>(<span class="hljs-number">100</span>, <span class="hljs-string">'result'</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
697  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res);  <span class="hljs-comment">// Prints 'result'</span>
698});</code><button class="copy-button">copy</button></pre>
699<h4><code>timersPromises.setImmediate([value[, options]])</code><span><a class="mark" href="#timerspromisessetimmediatevalue-options" id="timerspromisessetimmediatevalue-options">#</a></span><a aria-hidden="true" class="legacy" id="timers_timerspromises_setimmediate_value_options"></a></h4>
700<div class="api_metadata">
701<span>Added in: v15.0.0</span>
702</div>
703<ul>
704<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> A value with which the promise is fulfilled.</li>
705<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
706<ul>
707<li><code>ref</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Set to <code>false</code> to indicate that the scheduled <code>Immediate</code>
708should not require the Node.js event loop to remain active.
709<strong>Default:</strong> <code>true</code>.</li>
710<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An optional <code>AbortSignal</code> that can be used to
711cancel the scheduled <code>Immediate</code>.</li>
712</ul>
713</li>
714</ul>
715
716<pre class="with-15-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
717  setImmediate,
718} <span class="hljs-keyword">from</span> <span class="hljs-string">'timers/promises'</span>;
719
720<span class="hljs-keyword">const</span> res = <span class="hljs-keyword">await</span> <span class="hljs-title function_">setImmediate</span>(<span class="hljs-string">'result'</span>);
721
722<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res);  <span class="hljs-comment">// Prints 'result'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> {
723  setImmediate,
724} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
725
726<span class="hljs-title function_">setImmediate</span>(<span class="hljs-string">'result'</span>).<span class="hljs-title function_">then</span>(<span class="hljs-function">(<span class="hljs-params">res</span>) =></span> {
727  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res);  <span class="hljs-comment">// Prints 'result'</span>
728});</code><button class="copy-button">copy</button></pre>
729<h4><code>timersPromises.setInterval([delay[, value[, options]]])</code><span><a class="mark" href="#timerspromisessetintervaldelay-value-options" id="timerspromisessetintervaldelay-value-options">#</a></span><a aria-hidden="true" class="legacy" id="timers_timerspromises_setinterval_delay_value_options"></a></h4>
730<div class="api_metadata">
731<span>Added in: v15.9.0</span>
732</div>
733<p>Returns an async iterator that generates values in an interval of <code>delay</code> ms.
734If <code>ref</code> is <code>true</code>, you need to call <code>next()</code> of async iterator explicitly
735or implicitly to keep the event loop alive.</p>
736<ul>
737<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds to wait between iterations.
738<strong>Default:</strong> <code>1</code>.</li>
739<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> A value with which the iterator returns.</li>
740<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
741<ul>
742<li><code>ref</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Set to <code>false</code> to indicate that the scheduled <code>Timeout</code>
743between iterations should not require the Node.js event loop to
744remain active.
745<strong>Default:</strong> <code>true</code>.</li>
746<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An optional <code>AbortSignal</code> that can be used to
747cancel the scheduled <code>Timeout</code> between operations.</li>
748</ul>
749</li>
750</ul>
751
752<pre class="with-14-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> {
753  <span class="hljs-built_in">setInterval</span>,
754} <span class="hljs-keyword">from</span> <span class="hljs-string">'timers/promises'</span>;
755
756<span class="hljs-keyword">const</span> interval = <span class="hljs-number">100</span>;
757<span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> startTime <span class="hljs-keyword">of</span> <span class="hljs-built_in">setInterval</span>(interval, <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>())) {
758  <span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>();
759  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(now);
760  <span class="hljs-keyword">if</span> ((now - startTime) > <span class="hljs-number">1000</span>)
761    <span class="hljs-keyword">break</span>;
762}
763<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>());</code><code class="language-js cjs"><span class="hljs-keyword">const</span> {
764  <span class="hljs-built_in">setInterval</span>,
765} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
766<span class="hljs-keyword">const</span> interval = <span class="hljs-number">100</span>;
767
768(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) {
769  <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> startTime <span class="hljs-keyword">of</span> <span class="hljs-built_in">setInterval</span>(interval, <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>())) {
770    <span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>();
771    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(now);
772    <span class="hljs-keyword">if</span> ((now - startTime) > <span class="hljs-number">1000</span>)
773      <span class="hljs-keyword">break</span>;
774  }
775  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>());
776})();</code><button class="copy-button">copy</button></pre>
777<h4><code>timersPromises.scheduler.wait(delay[, options])</code><span><a class="mark" href="#timerspromisesschedulerwaitdelay-options" id="timerspromisesschedulerwaitdelay-options">#</a></span><a aria-hidden="true" class="legacy" id="timers_timerspromises_scheduler_wait_delay_options"></a></h4>
778<div class="api_metadata">
779<span>Added in: v17.3.0, v16.14.0</span>
780</div>
781<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
782<ul>
783<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The number of milliseconds to wait before resolving the
784promise.</li>
785<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
786<ul>
787<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type">&#x3C;AbortSignal></a> An optional <code>AbortSignal</code> that can be used to
788cancel waiting.</li>
789</ul>
790</li>
791<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
792</ul>
793<p>An experimental API defined by the <a href="https://github.com/WICG/scheduling-apis">Scheduling APIs</a> draft specification
794being developed as a standard Web Platform API.</p>
795<p>Calling <code>timersPromises.scheduler.wait(delay, options)</code> is roughly equivalent
796to calling <code>timersPromises.setTimeout(delay, undefined, options)</code> except that
797the <code>ref</code> option is not supported.</p>
798<pre><code class="language-js mjs"><span class="hljs-keyword">import</span> { scheduler } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>;
799
800<span class="hljs-keyword">await</span> scheduler.<span class="hljs-title function_">wait</span>(<span class="hljs-number">1000</span>); <span class="hljs-comment">// Wait one second before continuing</span></code> <button class="copy-button">copy</button></pre>
801<h4><code>timersPromises.scheduler.yield()</code><span><a class="mark" href="#timerspromisesscheduleryield" id="timerspromisesscheduleryield">#</a></span><a aria-hidden="true" class="legacy" id="timers_timerspromises_scheduler_yield"></a></h4>
802<div class="api_metadata">
803<span>Added in: v17.3.0, v16.14.0</span>
804</div>
805<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
806<ul>
807<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
808</ul>
809<p>An experimental API defined by the <a href="https://github.com/WICG/scheduling-apis">Scheduling APIs</a> draft specification
810being developed as a standard Web Platform API.</p>
811<p>Calling <code>timersPromises.scheduler.yield()</code> is equivalent to calling
812<code>timersPromises.setImmediate()</code> with no arguments.</p></section>
813        <!-- API END -->
814      </div>
815    </div>
816  </div>
817</body>
818</html>
819