CKTIS / JavaScript
setTimeout — schedule a function once
setTimeout schedules a function to run once without blocking the current code. The delay does not guarantee an exact execution time, and the returned identifier lets you cancel the pending invocation.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const id = setTimeout(callback, delayMs, ...args);
clearTimeout(id);setTimeout is an API defined by the HTML Standard for windows and workers, not a function of ECMAScript itself. It schedules a function once; setInterval schedules repetition. Supply a function callback, a delay delayMs in milliseconds, and optional args passed to the function. Pass a function rather than code text requiring compilation.
The call returns without waiting. Even with delay 0, the callback waits for a later event-loop task; it does not interrupt current synchronous code. Timing is not guaranteed, and execution can be later. When the timer nesting level exceeds 5, delays below 4 ms are raised to 4 ms.
Scheduling a function returns a positive integer identifier id. clearTimeout(id) cancels the pending invocation in the same global environment. It does not stop a callback already executing.
The example cancels one timer and passes "timer" as an argument to another. Synchronous code adds "end" before the timer callback runs, producing start -> end -> timer. Check ordering, not exact elapsed time.
Examples
(() => {
const events = ["start"];
const id = setTimeout(() => events.push("cancelled"), 0);
clearTimeout(id);
setTimeout((message) => {
events.push(message);
console.log(events.join(" -> "));
}, 0, "timer");
events.push("end");
})();Result
start -> end -> timer
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.