← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:d3zf97rf

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.},
}

Markdown

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.

All artifact files (JSON index)

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.