JavaScript Window clearInterval() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.clearInterval() cancels a repeating timer created by setInterval() (MDN). Pass the ID from setInterval. Unknown IDs are ignored.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.clearInterval. Start with this line.

JavaScript
clearInterval(intervalID)
CallWhat it does
clearInterval(intervalID)Primary signature.
window.clearInterval(...)Call with the arguments your case needs.

Built into the browser. Call window.clearInterval(...) (or the global clearInterval(...)) — no import required.

Library declarations
JavaScript
clearInterval(intervalID)

Parameters

ParameterDescription
intervalIDRequired. ID returned by setInterval()

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic Stop

Start an interval, then clear it immediately so it never fires.

JavaScript
const id = setInterval(function () {
  console.log("tick");
}, 1000);

clearInterval(id);
console.log("stopped"); // stopped (no ticks)
Try It Yourself

How It Works

  1. Schedule a timer. Save the ID from setInterval.
  2. Clear right away. Pass that ID to clearInterval.
  3. No ticks. The callback never runs.
  4. Return. clearInterval itself returns undefined.

Example 2 — Stop on Click

Keep a counter running until the user clicks Stop.

JavaScript
// Imagine buttons: Start / Stop and <pre id="out">
let id = null;
let n = 0;

function start() {
  if (id !== null) return;
  id = setInterval(function () {
    n += 1;
    document.getElementById("out").textContent = String(n);
  }, 400);
}

function stop() {
  clearInterval(id);
  id = null;
}
Try It Yourself

How It Works

  1. Start once. Guard with id !== null.
  2. Update UI each tick. Increment and display.
  3. Stop on demand. clearInterval(id).
  4. Reset the variable. Set id = null so Start can run again.

Example 3 — Auto-Stop

Clear from inside the callback after a fixed number of ticks.

JavaScript
let n = 0;
const id = setInterval(function () {
  n += 1;
  console.log(n);
  if (n >= 3) {
    clearInterval(id);
  }
}, 200);
// 1
// 2
// 3
Try It Yourself

How It Works

  1. Count ticks. Use a closed-over counter.
  2. Log each step. See 1, 2, 3.
  3. Clear at the limit. Call clearInterval(id) inside the callback.
  4. No fourth tick. The timer is cancelled.

Example 4 — Clear Twice

Calling clearInterval again with the same ID is safe.

JavaScript
const id = setInterval(function () {}, 1000);
clearInterval(id);
clearInterval(id); // no error
console.log("ok");
Try It Yourself

How It Works

  1. Start and clear. First clear stops the timer.
  2. Clear again. MDN: unknown / already cleared → no-op.
  3. No exception. Safe for defensive cleanup.
  4. Tip. Still set id = null in real apps for clarity.

Example 5 — Unknown ID

A fake ID does nothing — and does not throw.

JavaScript
clearInterval(999999);
console.log("still ok");
Try It Yourself

How It Works

  1. Pass a random ID. Not from setInterval.
  2. Browser ignores it. No matching timer.
  3. Script continues. No throw.
  4. Always store real IDs from setInterval in practice.

Browser Support

Window.clearInterval() is marked Baseline Widely available on MDN (since July 2015). Also available in workers.

✓ Baseline · Widely available

clearInterval()

Cancel repeating setInterval timers across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Safe for production. Always clear intervals you no longer need.

Frequently Asked Questions

It cancels a repeating timer previously started with setInterval(). After you clear that ID, the callback will not run again unless you create a new interval.
No. MDN marks Window.clearInterval() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The interval ID returned by setInterval(). Store that ID in a variable so you can pass it later.
undefined. It cancels the timer as a side effect; there is no useful return value.
Technically yes — setInterval and setTimeout share one ID pool, so clearInterval and clearTimeout can cancel either. Prefer matching names for clarity.
clearInterval() does nothing. Calling it twice or with an unknown ID is safe.

Next: clearTimeout()

Cancel a one-shot timer from setTimeout().

clearTimeout() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful