JavaScript Window clearTimeout() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.clearTimeout() cancels a one-shot timer created by setTimeout() (MDN). Pass the ID from setTimeout. 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.clearTimeout. Start with this line.

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

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

Library declarations
JavaScript
clearTimeout(timeoutID)

Parameters

ParameterDescription
timeoutIDRequired. ID returned by setTimeout()

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic Cancel

Schedule a timeout, then clear it immediately so it never fires.

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

clearTimeout(id);
console.log("cancelled"); // cancelled (no "hello")
Try It Yourself

How It Works

  1. Schedule a timer. Save the ID from setTimeout.
  2. Clear right away. Pass that ID to clearTimeout.
  3. No callback. "hello" never logs.
  4. Return. clearTimeout itself returns undefined.

Example 2 — Cancel on Click

Start a delayed message; cancel it before one second passes.

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

function start() {
  clearTimeout(id);
  document.getElementById("out").textContent = "Waiting 1s...";
  id = setTimeout(function () {
    document.getElementById("out").textContent = "Done!";
    id = null;
  }, 1000);
}

function cancel() {
  clearTimeout(id);
  id = null;
  document.getElementById("out").textContent = "Cancelled";
}
Try It Yourself

How It Works

  1. Start a delay. Store the timeout ID.
  2. Show waiting state. UI updates immediately.
  3. Cancel on demand. clearTimeout(id) before it fires.
  4. Reset the variable. Set id = null when done or cancelled.

Example 3 — Debounce Pattern

Each new call clears the previous timeout — only the last pause fires.

JavaScript
let id = null;

function onType(value) {
  clearTimeout(id);
  id = setTimeout(function () {
    console.log("search:", value);
  }, 300);
}

onType("c");
onType("co");
onType("cod");
// only "search: cod" logs (after 300ms quiet)
Try It Yourself

How It Works

  1. Clear the old timer. Every keystroke cancels the previous delay.
  2. Start a new delay. Fresh setTimeout with the latest value.
  3. Wait for quiet. When typing pauses, the last callback runs.
  4. Why it helps. Expensive work (search) runs once, not on every key.

Example 4 — Clear Twice

Calling clearTimeout again with the same ID is safe.

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

How It Works

  1. Start and clear. First clear cancels the timer.
  2. Clear again. MDN: invalid / 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
clearTimeout(999999);
console.log("still ok");
Try It Yourself

How It Works

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

Browser Support

Window.clearTimeout() is marked Baseline Widely available on MDN (since July 2015). Browser Window API — also available in workers.

✓ Baseline · Widely available

clearTimeout()

Cancel one-shot 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 everywhere you use setTimeout. Always clear pending timeouts that should not run.

Frequently Asked Questions

It cancels a one-shot timer previously started with setTimeout(). After you clear that ID, the callback will not run unless you schedule a new timeout.
No. MDN marks Window.clearTimeout() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The timeout ID returned by setTimeout(). 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 — setTimeout and setInterval share one ID pool, so clearTimeout and clearInterval can cancel either. Prefer matching names for clarity.
clearTimeout() does nothing. Calling it twice, after the callback ran, or with an unknown ID is safe.

Next: close()

Close the current browsing context when the browser allows it.

close() →

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.

9 people found this page helpful