setTimeout
A JavaScript function that runs a callback once after at least the given number of milliseconds, unless clearTimeout cancels it first.
In more detail
The delay is a minimum, not a promise: if the thread is busy the callback runs later, and it never runs early. Common uses include debouncing input and waiting briefly before showing a tooltip.
Examples
In the browser: Even setTimeout(fn, 0) waits until the current task and all microtasks have finished. Timers are not synchronised with the display, so animations should use requestAnimationFrame instead.In Node.js it returns a Timeout object with ref() and unref() methods, and a delay of 0 is treated as 1 millisecond.Starting a timer is a side effect: in React do it in an Effect or event handler and clear it in cleanup. Errors thrown in timer callbacks are not caught by error boundaries.Related terms
- tooltip — A small text label that appears when a control is hovered or focused, describing it briefly.
- microtask — A small job, such as a promise callback, that runs as soon as the current piece of JavaScript finishes, before the event loop handles anything else.
- setInterval() — A JavaScript function that runs a callback repeatedly, roughly every given number of milliseconds, until clearInterval stops it.
- macrotask — An ordinary task scheduled on the event loop, such as a timer or I/O callback, as opposed to a microtask.
- requestAnimationFrame() — A function that schedules a callback to run just before the browser's next repaint, the right place to update animations.
- timers phase — The first phase of each event loop turn, which runs the callbacks of setTimeout and setInterval timers that have expired.
- timer drift — The growing gap between when a timer was asked to fire and when it actually fires, caused by a busy thread or scheduling delays.
- unref() and ref() — Methods on Node.js timers, sockets and servers: unref() lets the process exit even though this handle is still active, and ref() undoes that.
- side effect — Anything a piece of code does besides returning a value, such as writing to a database, sending a message or changing a variable outside it.
- cleanup function — The function an Effect returns so React can undo what the setup did, such as removing a listener or cancelling a request.
- memory leak — Memory that a program keeps holding although it no longer needs it, so usage grows until the program slows down or crashes.
Taught in
- Overlay Architecture: Portals, Stacking Contexts & Backdrops — Design Systems & Component Engineering
- Floating Primitives: Popovers, Tooltips & Positioning — Design Systems & Component Engineering
- The Browser Event Loop: Call Stack, Microtasks And Macrotasks — Frontend Architecture & Web Platform
- requestAnimationFrame vs requestIdleCallback: High-FPS Work — Frontend Architecture & Web Platform
- Node.js Process Architecture: Memory & Heap Spaces — Node.js Internals & Backend Services
- Event Loop Lifecycle: The Six Phases & libuv uv_run — Node.js Internals & Backend Services
- Microtask Queues: process.nextTick vs Promise Timing — Node.js Internals & Backend Services
- Timers Phase: setTimeout, Min-Heap Scheduling & Drift — Node.js Internals & Backend Services
- Poll & Check Phases: I/O Callbacks & setImmediate — Node.js Internals & Backend Services
- Component Purity: Pure Functions, Idempotence And Side Effects — React 19 & Modern Component Architecture
- State with useState: Immutability And Asynchronous Batching — React 19 & Modern Component Architecture
- Complex State with useReducer: State Machines And Action Types — React 19 & Modern Component Architecture
- Diffing Algorithm: Element Type Shifts And Subtree Tear Down — React 19 & Modern Component Architecture
- useEffect Lifecycle: Mount, Dependencies, And Cleanup Phases — React 19 & Modern Component Architecture
- useRef Mechanics: DOM References And Mutable Persistent State — React 19 & Modern Component Architecture
- Error Boundaries: componentDidCatch And Fallback UI Trees — React 19 & Modern Component Architecture