Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

clearInterval — canceling a repeating timer

clearInterval(id) cancels the timer identified by the value returned from setInterval(). It stops subsequent invocations but does not interrupt a running function.

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

clearInterval(id)

clearInterval(id) removes a repeating timer. Keep the numeric identifier returned by setInterval() and pass it in the same global environment. This is an HTML-standard Web API available in windows and workers, not a function defined by ECMAScript. The standard lists support in all current browser engines.

Cancellation prevents subsequent invocations but does not interrupt the currently executing function or undo its effects. A nonexistent identifier changes nothing; canceling again without reuse of that identifier is harmless. The call returns undefined.

clearInterval() and clearTimeout() share an identifier map, so either can cancel either kind of timer. For readability, nevertheless pair setInterval() with clearInterval(). Identifiers belong to a particular global environment, not to all windows and workers collectively.

The example cancels the timer inside its third invocation. Code after clearInterval(id) still prints stopped. The delay 20 does not guarantee an exact time interval; verify output order rather than arrival times.

Examples

(() => {
  let count = 0;
  const id = setInterval(() => {
    count += 1;
    console.log(count);
    if (count === 3) {
      clearInterval(id);
      console.log("stopped");
    }
  }, 20);
})();

Result

1
2
3
stopped

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources