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

Taught in

References