CKTIS / JavaScript
clearTimeout — cancelling a timer
clearTimeout(id) cancels a pending callback scheduled with setTimeout(). To cancel it, pass the matching timer identifier, not the callback function. The argument is optional: calling clearTimeout() without it uses the default value 0 and does nothing.
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
clearTimeout()
clearTimeout(id)clearTimeout(id) cancels the timer identified by the value returned by setTimeout(). It returns undefined. It is an HTML-standard API available in browser windows and workers, not part of ECMAScript itself. The standard reports support in all current browser engines; the presence of Node 24 does not establish browser API support.
Keep the identifier and cancel the timer in the same global environment where it was created. An identifier absent from the timer registry changes nothing. Cancellation does not interrupt a callback already executing or undo its effects.
clearTimeout() and clearInterval() share a registry, so technically either can cancel both kinds of timer. For readability, pair setTimeout() with clearTimeout() and setInterval() with clearInterval().
In the example, cancellation happens synchronously before the callback can execute. A delay of 0 does not mean immediate execution, so the only logged message is cancelled.
Examples
(() => {
const id = setTimeout(() => console.log("unexpected"), 0);
clearTimeout(id);
console.log("cancelled");
})();Result
cancelled
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.