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.18.2"> 7 <title>Timers | Node.js v18.18.2 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.18.2 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.18.2</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</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">►</span><span class="expanded-arrow">▼</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">►</span><span class="expanded-arrow">▼</span> 261 Other versions 262 </a> 263 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/timers.html">20.x</a></li> 264<li><a href="https://nodejs.org/docs/latest-v19.x/api/timers.html">19.x</a></li> 265<li><a href="https://nodejs.org/docs/latest-v18.x/api/timers.html">18.x <b>LTS</b></a></li> 266<li><a href="https://nodejs.org/docs/latest-v17.x/api/timers.html">17.x</a></li> 267<li><a href="https://nodejs.org/docs/latest-v16.x/api/timers.html">16.x</a></li> 268<li><a href="https://nodejs.org/docs/latest-v15.x/api/timers.html">15.x</a></li> 269<li><a href="https://nodejs.org/docs/latest-v14.x/api/timers.html">14.x</a></li> 270<li><a href="https://nodejs.org/docs/latest-v13.x/api/timers.html">13.x</a></li> 271<li><a href="https://nodejs.org/docs/latest-v12.x/api/timers.html">12.x</a></li> 272<li><a href="https://nodejs.org/docs/latest-v11.x/api/timers.html">11.x</a></li> 273<li><a href="https://nodejs.org/docs/latest-v10.x/api/timers.html">10.x</a></li> 274<li><a href="https://nodejs.org/docs/latest-v9.x/api/timers.html">9.x</a></li> 275<li><a href="https://nodejs.org/docs/latest-v8.x/api/timers.html">8.x</a></li> 276<li><a href="https://nodejs.org/docs/latest-v7.x/api/timers.html">7.x</a></li> 277<li><a href="https://nodejs.org/docs/latest-v6.x/api/timers.html">6.x</a></li> 278<li><a href="https://nodejs.org/docs/latest-v5.x/api/timers.html">5.x</a></li> 279<li><a href="https://nodejs.org/docs/latest-v4.x/api/timers.html">4.x</a></li> 280<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/timers.html">0.12.x</a></li> 281<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/timers.html">0.10.x</a></li></ol></div> 282 </li> 283 284 <li class="picker-header"> 285 <a href="#"> 286 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 287 Options 288 </a> 289 290 <div class="picker"> 291 <ul> 292 <li> 293 <a href="all.html">View on single page</a> 294 </li> 295 <li> 296 <a href="timers.json">View as JSON</a> 297 </li> 298 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/timers.md">Edit on GitHub</a></li> 299 </ul> 300 </div> 301 </li> 302 </ul> 303 </div> 304 <hr> 305 </header> 306 307 <details id="toc" open><summary>Table of contents</summary><ul> 308<li><span class="stability_2"><a href="#timers">Timers</a></span> 309<ul> 310<li><a href="#class-immediate">Class: <code>Immediate</code></a> 311<ul> 312<li><a href="#immediatehasref"><code>immediate.hasRef()</code></a></li> 313<li><a href="#immediateref"><code>immediate.ref()</code></a></li> 314<li><a href="#immediateunref"><code>immediate.unref()</code></a></li> 315<li><span class="stability_1"><a href="#immediatesymboldispose"><code>immediate[Symbol.dispose]()</code></a></span></li> 316</ul> 317</li> 318<li><a href="#class-timeout">Class: <code>Timeout</code></a> 319<ul> 320<li><span class="stability_3"><a href="#timeoutclose"><code>timeout.close()</code></a></span></li> 321<li><a href="#timeouthasref"><code>timeout.hasRef()</code></a></li> 322<li><a href="#timeoutref"><code>timeout.ref()</code></a></li> 323<li><a href="#timeoutrefresh"><code>timeout.refresh()</code></a></li> 324<li><a href="#timeoutunref"><code>timeout.unref()</code></a></li> 325<li><a href="#timeoutsymboltoprimitive"><code>timeout[Symbol.toPrimitive]()</code></a></li> 326<li><span class="stability_1"><a href="#timeoutsymboldispose"><code>timeout[Symbol.dispose]()</code></a></span></li> 327</ul> 328</li> 329<li><a href="#scheduling-timers">Scheduling timers</a> 330<ul> 331<li><a href="#setimmediatecallback-args"><code>setImmediate(callback[, ...args])</code></a></li> 332<li><a href="#setintervalcallback-delay-args"><code>setInterval(callback[, delay[, ...args]])</code></a></li> 333<li><a href="#settimeoutcallback-delay-args"><code>setTimeout(callback[, delay[, ...args]])</code></a></li> 334</ul> 335</li> 336<li><a href="#cancelling-timers">Cancelling timers</a> 337<ul> 338<li><a href="#clearimmediateimmediate"><code>clearImmediate(immediate)</code></a></li> 339<li><a href="#clearintervaltimeout"><code>clearInterval(timeout)</code></a></li> 340<li><a href="#cleartimeouttimeout"><code>clearTimeout(timeout)</code></a></li> 341</ul> 342</li> 343<li><a href="#timers-promises-api">Timers Promises API</a> 344<ul> 345<li><a href="#timerspromisessettimeoutdelay-value-options"><code>timersPromises.setTimeout([delay[, value[, options]]])</code></a></li> 346<li><a href="#timerspromisessetimmediatevalue-options"><code>timersPromises.setImmediate([value[, options]])</code></a></li> 347<li><a href="#timerspromisessetintervaldelay-value-options"><code>timersPromises.setInterval([delay[, value[, options]]])</code></a></li> 348<li><span class="stability_1"><a href="#timerspromisesschedulerwaitdelay-options"><code>timersPromises.scheduler.wait(delay[, options])</code></a></span></li> 349<li><span class="stability_1"><a href="#timerspromisesscheduleryield"><code>timersPromises.scheduler.yield()</code></a></span></li> 350</ul> 351</li> 352</ul> 353</li> 354</ul></details> 355 356 <div id="apicontent"> 357 <h2>Timers<span><a class="mark" href="#timers" id="timers">#</a></span><a aria-hidden="true" class="legacy" id="timers_timers"></a></h2> 358 359<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 360<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/timers.js">lib/timers.js</a></p> 361<p>The <code>timer</code> module exposes a global API for scheduling functions to 362be called at some future period of time. Because the timer functions are 363globals, there is no need to call <code>require('node:timers')</code> to use the API.</p> 364<p>The timer functions within Node.js implement a similar API as the timers API 365provided by Web Browsers but use a different internal implementation that is 366built around the Node.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout">Event Loop</a>.</p> 367<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> 368<p>This object is created internally and is returned from <a href="#setimmediatecallback-args"><code>setImmediate()</code></a>. It 369can be passed to <a href="#clearimmediateimmediate"><code>clearImmediate()</code></a> in order to cancel the scheduled 370actions.</p> 371<p>By default, when an immediate is scheduled, the Node.js event loop will continue 372running as long as the immediate is active. The <code>Immediate</code> object returned by 373<a href="#setimmediatecallback-args"><code>setImmediate()</code></a> exports both <code>immediate.ref()</code> and <code>immediate.unref()</code> 374functions that can be used to control this default behavior.</p> 375<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> 376<div class="api_metadata"> 377<span>Added in: v11.0.0</span> 378</div> 379<ul> 380<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 381</ul> 382<p>If true, the <code>Immediate</code> object will keep the Node.js event loop active.</p> 383<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> 384<div class="api_metadata"> 385<span>Added in: v9.7.0</span> 386</div> 387<ul> 388<li>Returns: <a href="timers.html#class-immediate" class="type"><Immediate></a> a reference to <code>immediate</code></li> 389</ul> 390<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the 391<code>Immediate</code> is active. Calling <code>immediate.ref()</code> multiple times will have no 392effect.</p> 393<p>By default, all <code>Immediate</code> objects are "ref'ed", making it normally unnecessary 394to call <code>immediate.ref()</code> unless <code>immediate.unref()</code> had been called previously.</p> 395<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> 396<div class="api_metadata"> 397<span>Added in: v9.7.0</span> 398</div> 399<ul> 400<li>Returns: <a href="timers.html#class-immediate" class="type"><Immediate></a> a reference to <code>immediate</code></li> 401</ul> 402<p>When called, the active <code>Immediate</code> object will not require the Node.js event 403loop to remain active. If there is no other activity keeping the event loop 404running, the process may exit before the <code>Immediate</code> object's callback is 405invoked. Calling <code>immediate.unref()</code> multiple times will have no effect.</p> 406<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> 407<div class="api_metadata"> 408<span>Added in: v18.18.0</span> 409</div> 410<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 411<p>Cancels the immediate. This is similar to calling <code>clearImmediate()</code>.</p> 412</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> 413<p>This object is created internally and is returned from <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a> and 414<a href="#setintervalcallback-delay-args"><code>setInterval()</code></a>. It can be passed to either <a href="#cleartimeouttimeout"><code>clearTimeout()</code></a> or 415<a href="#clearintervaltimeout"><code>clearInterval()</code></a> in order to cancel the scheduled actions.</p> 416<p>By default, when a timer is scheduled using either <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a> or 417<a href="#setintervalcallback-delay-args"><code>setInterval()</code></a>, the Node.js event loop will continue running as long as the 418timer is active. Each of the <code>Timeout</code> objects returned by these functions 419export both <code>timeout.ref()</code> and <code>timeout.unref()</code> functions that can be used to 420control this default behavior.</p> 421<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> 422<div class="api_metadata"> 423<span>Added in: v0.9.1</span> 424</div> 425<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> 426<ul> 427<li>Returns: <a href="timers.html#class-timeout" class="type"><Timeout></a> a reference to <code>timeout</code></li> 428</ul> 429<p>Cancels the timeout.</p> 430<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> 431<div class="api_metadata"> 432<span>Added in: v11.0.0</span> 433</div> 434<ul> 435<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 436</ul> 437<p>If true, the <code>Timeout</code> object will keep the Node.js event loop active.</p> 438<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> 439<div class="api_metadata"> 440<span>Added in: v0.9.1</span> 441</div> 442<ul> 443<li>Returns: <a href="timers.html#class-timeout" class="type"><Timeout></a> a reference to <code>timeout</code></li> 444</ul> 445<p>When called, requests that the Node.js event loop <em>not</em> exit so long as the 446<code>Timeout</code> is active. Calling <code>timeout.ref()</code> multiple times will have no effect.</p> 447<p>By default, all <code>Timeout</code> objects are "ref'ed", making it normally unnecessary 448to call <code>timeout.ref()</code> unless <code>timeout.unref()</code> had been called previously.</p> 449<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> 450<div class="api_metadata"> 451<span>Added in: v10.2.0</span> 452</div> 453<ul> 454<li>Returns: <a href="timers.html#class-timeout" class="type"><Timeout></a> a reference to <code>timeout</code></li> 455</ul> 456<p>Sets the timer's start time to the current time, and reschedules the timer to 457call its callback at the previously specified duration adjusted to the current 458time. This is useful for refreshing a timer without allocating a new 459JavaScript object.</p> 460<p>Using this on a timer that has already called its callback will reactivate the 461timer.</p> 462<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> 463<div class="api_metadata"> 464<span>Added in: v0.9.1</span> 465</div> 466<ul> 467<li>Returns: <a href="timers.html#class-timeout" class="type"><Timeout></a> a reference to <code>timeout</code></li> 468</ul> 469<p>When called, the active <code>Timeout</code> object will not require the Node.js event loop 470to remain active. If there is no other activity keeping the event loop running, 471the process may exit before the <code>Timeout</code> object's callback is invoked. Calling 472<code>timeout.unref()</code> multiple times will have no effect.</p> 473<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> 474<div class="api_metadata"> 475<span>Added in: v14.9.0, v12.19.0</span> 476</div> 477<ul> 478<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> a number that can be used to reference this <code>timeout</code></li> 479</ul> 480<p>Coerce a <code>Timeout</code> to a primitive. The primitive can be used to 481clear the <code>Timeout</code>. The primitive can only be used in the 482same thread where the timeout was created. Therefore, to use it 483across <a href="worker_threads.html"><code>worker_threads</code></a> it must first be passed to the correct 484thread. This allows enhanced compatibility with browser 485<code>setTimeout()</code> and <code>setInterval()</code> implementations.</p> 486<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> 487<div class="api_metadata"> 488<span>Added in: v18.18.0</span> 489</div> 490<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 491<p>Cancels the timeout.</p> 492</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> 493<p>A timer in Node.js is an internal construct that calls a given function after 494a certain period of time. When a timer's function is called varies depending on 495which method was used to create the timer and what other work the Node.js 496event loop is doing.</p> 497<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> 498<div class="api_metadata"> 499<details class="changelog"><summary>History</summary> 500<table> 501<tbody><tr><th>Version</th><th>Changes</th></tr> 502<tr><td>v18.0.0</td> 503<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> 504<tr><td>v0.9.1</td> 505<td><p><span>Added in: v0.9.1</span></p></td></tr> 506</tbody></table> 507</details> 508</div> 509<ul> 510<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to call at the end of this turn of 511the Node.js <a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#setimmediate-vs-settimeout">Event Loop</a></li> 512<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional arguments to pass when the <code>callback</code> is called.</li> 513<li>Returns: <a href="timers.html#class-immediate" class="type"><Immediate></a> for use with <a href="#clearimmediateimmediate"><code>clearImmediate()</code></a></li> 514</ul> 515<p>Schedules the "immediate" execution of the <code>callback</code> after I/O events' 516callbacks.</p> 517<p>When multiple calls to <code>setImmediate()</code> are made, the <code>callback</code> functions are 518queued for execution in the order in which they are created. The entire callback 519queue is processed every event loop iteration. If an immediate timer is queued 520from inside an executing callback, that timer will not be triggered until the 521next event loop iteration.</p> 522<p>If <code>callback</code> is not a function, a <a href="errors.html#class-typeerror"><code>TypeError</code></a> will be thrown.</p> 523<p>This method has a custom variant for promises that is available using 524<a href="#timerspromisessetimmediatevalue-options"><code>timersPromises.setImmediate()</code></a>.</p> 525<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> 526<div class="api_metadata"> 527<details class="changelog"><summary>History</summary> 528<table> 529<tbody><tr><th>Version</th><th>Changes</th></tr> 530<tr><td>v18.0.0</td> 531<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> 532<tr><td>v0.0.1</td> 533<td><p><span>Added in: v0.0.1</span></p></td></tr> 534</tbody></table> 535</details> 536</div> 537<ul> 538<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to call when the timer elapses.</li> 539<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds to wait before calling the 540<code>callback</code>. <strong>Default:</strong> <code>1</code>.</li> 541<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional arguments to pass when the <code>callback</code> is called.</li> 542<li>Returns: <a href="timers.html#class-timeout" class="type"><Timeout></a> for use with <a href="#clearintervaltimeout"><code>clearInterval()</code></a></li> 543</ul> 544<p>Schedules repeated execution of <code>callback</code> every <code>delay</code> milliseconds.</p> 545<p>When <code>delay</code> is larger than <code>2147483647</code> or less than <code>1</code>, the <code>delay</code> will be 546set to <code>1</code>. Non-integer delays are truncated to an integer.</p> 547<p>If <code>callback</code> is not a function, a <a href="errors.html#class-typeerror"><code>TypeError</code></a> will be thrown.</p> 548<p>This method has a custom variant for promises that is available using 549<a href="#timerspromisessetintervaldelay-value-options"><code>timersPromises.setInterval()</code></a>.</p> 550<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> 551<div class="api_metadata"> 552<details class="changelog"><summary>History</summary> 553<table> 554<tbody><tr><th>Version</th><th>Changes</th></tr> 555<tr><td>v18.0.0</td> 556<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> 557<tr><td>v0.0.1</td> 558<td><p><span>Added in: v0.0.1</span></p></td></tr> 559</tbody></table> 560</details> 561</div> 562<ul> 563<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to call when the timer elapses.</li> 564<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds to wait before calling the 565<code>callback</code>. <strong>Default:</strong> <code>1</code>.</li> 566<li><code>...args</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Optional arguments to pass when the <code>callback</code> is called.</li> 567<li>Returns: <a href="timers.html#class-timeout" class="type"><Timeout></a> for use with <a href="#cleartimeouttimeout"><code>clearTimeout()</code></a></li> 568</ul> 569<p>Schedules execution of a one-time <code>callback</code> after <code>delay</code> milliseconds.</p> 570<p>The <code>callback</code> will likely not be invoked in precisely <code>delay</code> milliseconds. 571Node.js makes no guarantees about the exact timing of when callbacks will fire, 572nor of their ordering. The callback will be called as close as possible to the 573time specified.</p> 574<p>When <code>delay</code> is larger than <code>2147483647</code> or less than <code>1</code>, the <code>delay</code> 575will be set to <code>1</code>. Non-integer delays are truncated to an integer.</p> 576<p>If <code>callback</code> is not a function, a <a href="errors.html#class-typeerror"><code>TypeError</code></a> will be thrown.</p> 577<p>This method has a custom variant for promises that is available using 578<a href="#timerspromisessettimeoutdelay-value-options"><code>timersPromises.setTimeout()</code></a>.</p> 579</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> 580<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 581each return objects that represent the scheduled timers. These can be used to 582cancel the timer and prevent it from triggering.</p> 583<p>For the promisified variants of <a href="#setimmediatecallback-args"><code>setImmediate()</code></a> and <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a>, 584an <a href="globals.html#class-abortcontroller"><code>AbortController</code></a> may be used to cancel the timer. When canceled, the 585returned Promises will be rejected with an <code>'AbortError'</code>.</p> 586<p>For <code>setImmediate()</code>:</p> 587<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>); 588 589<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 590<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>; 591 592<span class="hljs-title function_">setImmediatePromise</span>(<span class="hljs-string">'foobar'</span>, { signal }) 593 .<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>) 594 .<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 595 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) 596 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'The immediate was aborted'</span>); 597 }); 598 599ac.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> 600<p>For <code>setTimeout()</code>:</p> 601<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>); 602 603<span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 604<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>; 605 606setTimeoutPromise(<span class="hljs-number">1000</span>, <span class="hljs-string">'foobar'</span>, { signal }) 607 .<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>) 608 .<span class="hljs-title function_">catch</span>(<span class="hljs-function">(<span class="hljs-params">err</span>) =></span> { 609 <span class="hljs-keyword">if</span> (err.<span class="hljs-property">name</span> === <span class="hljs-string">'AbortError'</span>) 610 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'The timeout was aborted'</span>); 611 }); 612 613ac.<span class="hljs-title function_">abort</span>();</code> <button class="copy-button">copy</button></pre> 614<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> 615<div class="api_metadata"> 616<span>Added in: v0.9.1</span> 617</div> 618<ul> 619<li><code>immediate</code> <a href="timers.html#class-immediate" class="type"><Immediate></a> An <code>Immediate</code> object as returned by 620<a href="#setimmediatecallback-args"><code>setImmediate()</code></a>.</li> 621</ul> 622<p>Cancels an <code>Immediate</code> object created by <a href="#setimmediatecallback-args"><code>setImmediate()</code></a>.</p> 623<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> 624<div class="api_metadata"> 625<span>Added in: v0.0.1</span> 626</div> 627<ul> 628<li><code>timeout</code> <a href="timers.html#class-timeout" class="type"><Timeout></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A <code>Timeout</code> object as returned by <a href="#setintervalcallback-delay-args"><code>setInterval()</code></a> 629or the <a href="#timeoutsymboltoprimitive">primitive</a> of the <code>Timeout</code> object as a string or a number.</li> 630</ul> 631<p>Cancels a <code>Timeout</code> object created by <a href="#setintervalcallback-delay-args"><code>setInterval()</code></a>.</p> 632<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> 633<div class="api_metadata"> 634<span>Added in: v0.0.1</span> 635</div> 636<ul> 637<li><code>timeout</code> <a href="timers.html#class-timeout" class="type"><Timeout></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> A <code>Timeout</code> object as returned by <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a> 638or the <a href="#timeoutsymboltoprimitive">primitive</a> of the <code>Timeout</code> object as a string or a number.</li> 639</ul> 640<p>Cancels a <code>Timeout</code> object created by <a href="#settimeoutcallback-delay-args"><code>setTimeout()</code></a>.</p> 641</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> 642<div class="api_metadata"> 643<details class="changelog"><summary>History</summary> 644<table> 645<tbody><tr><th>Version</th><th>Changes</th></tr> 646<tr><td>v16.0.0</td> 647<td><p>Graduated from experimental.</p></td></tr> 648<tr><td>v15.0.0</td> 649<td><p><span>Added in: v15.0.0</span></p></td></tr> 650</tbody></table> 651</details> 652</div> 653<p>The <code>timers/promises</code> API provides an alternative set of timer functions 654that return <code>Promise</code> objects. The API is accessible via 655<code>require('node:timers/promises')</code>.</p> 656 657<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> { 658 <span class="hljs-built_in">setTimeout</span>, 659 setImmediate, 660 <span class="hljs-built_in">setInterval</span>, 661} <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> { 662 <span class="hljs-built_in">setTimeout</span>, 663 setImmediate, 664 <span class="hljs-built_in">setInterval</span>, 665} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);</code><button class="copy-button">copy</button></pre> 666<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> 667<div class="api_metadata"> 668<span>Added in: v15.0.0</span> 669</div> 670<ul> 671<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds to wait before fulfilling the 672promise. <strong>Default:</strong> <code>1</code>.</li> 673<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> A value with which the promise is fulfilled.</li> 674<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 675<ul> 676<li><code>ref</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Set to <code>false</code> to indicate that the scheduled <code>Timeout</code> 677should not require the Node.js event loop to remain active. 678<strong>Default:</strong> <code>true</code>.</li> 679<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An optional <code>AbortSignal</code> that can be used to 680cancel the scheduled <code>Timeout</code>.</li> 681</ul> 682</li> 683</ul> 684 685<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> { 686 <span class="hljs-built_in">setTimeout</span>, 687} <span class="hljs-keyword">from</span> <span class="hljs-string">'timers/promises'</span>; 688 689<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>); 690 691<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> { 692 <span class="hljs-built_in">setTimeout</span>, 693} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 694 695<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> { 696 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res); <span class="hljs-comment">// Prints 'result'</span> 697});</code><button class="copy-button">copy</button></pre> 698<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> 699<div class="api_metadata"> 700<span>Added in: v15.0.0</span> 701</div> 702<ul> 703<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> A value with which the promise is fulfilled.</li> 704<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 705<ul> 706<li><code>ref</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Set to <code>false</code> to indicate that the scheduled <code>Immediate</code> 707should not require the Node.js event loop to remain active. 708<strong>Default:</strong> <code>true</code>.</li> 709<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An optional <code>AbortSignal</code> that can be used to 710cancel the scheduled <code>Immediate</code>.</li> 711</ul> 712</li> 713</ul> 714 715<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> { 716 setImmediate, 717} <span class="hljs-keyword">from</span> <span class="hljs-string">'timers/promises'</span>; 718 719<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>); 720 721<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> { 722 setImmediate, 723} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 724 725<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> { 726 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(res); <span class="hljs-comment">// Prints 'result'</span> 727});</code><button class="copy-button">copy</button></pre> 728<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> 729<div class="api_metadata"> 730<span>Added in: v15.9.0</span> 731</div> 732<p>Returns an async iterator that generates values in an interval of <code>delay</code> ms. 733If <code>ref</code> is <code>true</code>, you need to call <code>next()</code> of async iterator explicitly 734or implicitly to keep the event loop alive.</p> 735<ul> 736<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds to wait between iterations. 737<strong>Default:</strong> <code>1</code>.</li> 738<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> A value with which the iterator returns.</li> 739<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 740<ul> 741<li><code>ref</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Set to <code>false</code> to indicate that the scheduled <code>Timeout</code> 742between iterations should not require the Node.js event loop to 743remain active. 744<strong>Default:</strong> <code>true</code>.</li> 745<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An optional <code>AbortSignal</code> that can be used to 746cancel the scheduled <code>Timeout</code> between operations.</li> 747</ul> 748</li> 749</ul> 750 751<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> { 752 <span class="hljs-built_in">setInterval</span>, 753} <span class="hljs-keyword">from</span> <span class="hljs-string">'timers/promises'</span>; 754 755<span class="hljs-keyword">const</span> interval = <span class="hljs-number">100</span>; 756<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>())) { 757 <span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(); 758 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(now); 759 <span class="hljs-keyword">if</span> ((now - startTime) > <span class="hljs-number">1000</span>) 760 <span class="hljs-keyword">break</span>; 761} 762<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> { 763 <span class="hljs-built_in">setInterval</span>, 764} = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>); 765<span class="hljs-keyword">const</span> interval = <span class="hljs-number">100</span>; 766 767(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { 768 <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>())) { 769 <span class="hljs-keyword">const</span> now = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>(); 770 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(now); 771 <span class="hljs-keyword">if</span> ((now - startTime) > <span class="hljs-number">1000</span>) 772 <span class="hljs-keyword">break</span>; 773 } 774 <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>()); 775})();</code><button class="copy-button">copy</button></pre> 776<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> 777<div class="api_metadata"> 778<span>Added in: v17.3.0, v16.14.0</span> 779</div> 780<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 781<ul> 782<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The number of milliseconds to wait before resolving the 783promise.</li> 784<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 785<ul> 786<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> An optional <code>AbortSignal</code> that can be used to 787cancel waiting.</li> 788</ul> 789</li> 790<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 791</ul> 792<p>An experimental API defined by the <a href="https://github.com/WICG/scheduling-apis">Scheduling APIs</a> draft specification 793being developed as a standard Web Platform API.</p> 794<p>Calling <code>timersPromises.scheduler.wait(delay, options)</code> is roughly equivalent 795to calling <code>timersPromises.setTimeout(delay, undefined, options)</code> except that 796the <code>ref</code> option is not supported.</p> 797<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>; 798 799<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> 800<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> 801<div class="api_metadata"> 802<span>Added in: v17.3.0, v16.14.0</span> 803</div> 804<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 805<ul> 806<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 807</ul> 808<p>An experimental API defined by the <a href="https://github.com/WICG/scheduling-apis">Scheduling APIs</a> draft specification 809being developed as a standard Web Platform API.</p> 810<p>Calling <code>timersPromises.scheduler.yield()</code> is equivalent to calling 811<code>timersPromises.setImmediate()</code> with no arguments.</p></section> 812 <!-- API END --> 813 </div> 814 </div> 815 </div> 816</body> 817</html> 818