Node.js timers - setTimeout, setInterval, setImmediate, unref and timers/promises
Answer: Use setTimeout() for a one-time callback after a delay in milliseconds, setInterval() for repeated execution, and setImmediate() to run after I/O events' callbacks. The returned objects can be used to cancel the timer. For the promisified variants in node:timers/promises, an AbortController may be used to cancel the timer.
Examples
const t = setTimeout((name) => console.log('hi', name), 500, 'ada');
clearTimeout(t); // cancel before it fires
const iv = setInterval(() => console.log('tick'), 1000);
iv.unref(); // do not keep the process alive
setImmediate(() => console.log('after I/O callbacks'));
// Reset an idle timer without a new object
const idle = setTimeout(() => console.log('idle'), 5000);
idle.refresh();
// Promise variants
import { setTimeout as sleep, setInterval as every } from 'node:timers/promises';
await sleep(200);
const ac = new AbortController();
sleep(10_000, 'done', { signal: ac.signal })
.catch((err) => { if (err.name === 'AbortError') console.log('aborted'); });
ac.abort();
for await (const _ of every(1000)) { break; }
Functions
| Function | Schedules | Cancel with | |---|---|---| | setTimeout(cb, delay, ...args) | execution of a one-time callback after delay milliseconds | clearTimeout() | | setInterval(cb, delay, ...args) | repeated execution of callback every delay milliseconds | clearInterval() | | setImmediate(cb, ...args) | execution of the callback after I/O events' callbacks | clearImmediate() |
Rules
- Node.js makes no guarantees about the exact timing of when callbacks will fire.
- When delay is larger than 2147483647 or less than 1, the delay will be set to 1. Non-integer delays are truncated.
- A non-function callback throws a TypeError.
- By default an active timer keeps the event loop running.
unref()lets the process exit if there is no other activity;ref()restores the default. timeout.refresh()resets the start time to the current time and reschedules the timer.- When the promisified variants are canceled, the returned Promises are rejected with an 'AbortError'.
See also
Claims
- id: C1 claim: setInterval schedules repeated execution. source: S1 evidence: "Schedules repeated execution of callback every delay milliseconds."
- id: C2 claim: Exact timing is not guaranteed. source: S1 evidence: "Node.js makes no guarantees about the exact timing of when callbacks will fire"
- id: C3 claim: The module schedules functions for later. source: S2 evidence: "module exposes a global API for scheduling functions to"
Sources
- [S1] Timers. Node.js API docs. https://nodejs.org/api/timers.html . License: MIT. Accessed 2026-11-18.
- [S2] doc/api/timers.md. nodejs/node (GitHub). https://github.com/nodejs/node/blob/main/doc/api/timers.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Timers; nodejs/node (GitHub), doc/api/timers.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Timers; nodejs/node (GitHub), doc/api/timers.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.
Cite this
iinventedlight. 2026. "Node.js timers - setTimeout, setInterval, setImmediate, unref and timers/promises." iinventedlight Library, iil:note:d3zf97rf, version of 2026-11-18. https://iinventedlight.com/library/notes/node-timers/. License: CC-BY-4.0.
@misc{iil_note_d3zf97rf,
author = {{iinventedlight}},
title = {{Node.js timers - setTimeout, setInterval, setImmediate, unref and timers/promises}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/node-timers/},
note = {Permanent ID iil:note:d3zf97rf. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- node-timers.table.json (table; CC-BY-4.0)
- node-timers.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-timers.md.