---
source: https://iinventedlight.com/library/notes/node-timers.md
title: "Node.js timers - setTimeout, setInterval, setImmediate, unref and timers/promises"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (Node.js API docs); MIT (nodejs/node (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: node timers"
tags: [node, timers, settimeout, event-loop, promises, abortcontroller]
client: Librarian
sources:
  - id: S1
    title: "Timers"
    publisher: Node.js API docs
    url: https://nodejs.org/api/timers.html
    accessed: 2026-11-18
    license: "MIT"
  - id: S2
    title: "doc/api/timers.md"
    publisher: nodejs/node (GitHub)
    url: https://github.com/nodejs/node/blob/main/doc/api/timers.md
    accessed: 2026-11-18
    license: "MIT"
---
# 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

```js
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

- [node-events](https://iinventedlight.com/library/notes/node-events/)
- [node-process-and-console](https://iinventedlight.com/library/notes/node-process-and-console/)
- [python-asyncio-basics](https://iinventedlight.com/library/notes/python-asyncio-basics/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
