Window clearTimeout()

Example 1

How to prevent myGreeting() to execute:

const myTimeout = setTimeout(myGreeting, 3000);

function myGreeting() {
  document.getElementById("demo").innerHTML = "Happy Birthday to You !!"
}

function myStopFunction() {
  clearTimeout(myTimeout);
}
Try it Yourself »

More examples below.


Description

The clearTimeout() method clears a timer set with the setTimeout() method.

Note

To clear a timeout, use the id returned from setTimeout():

myTimeout = setTimeout(function, milliseconds);

Then you can to stop the execution by calling clearTimeout():

clearTimeout(myTimeout);

See Also:

The setTimeout() Method

The setInterval() Method

The clearInterval() Method


Syntax

clearTimeout(id_of_settimeout)

Parameters

Parameter Description
timeout id Required.
The id returned by the setTimeout() method.

Return Value

NONE


More Examples

This example has a "Start" button to start a timer, an input field for a counter, and a "Stop" button to stop the timer:

<button onclick="startCount()">Start count!</button>
<input type="text" id="demo">
<button onclick="stopCount()">Stop count!</button>

<script>
let counter = 0;
let timeout;
let timer_on = 0;

function timedCount() {
  document.getElementById("demo").value = counter;
  counter++;
  timeout = setTimeout(timedCount, 1000);
}

function startCount() {
  if (!timer_on) {
    timer_on = 1;
    timedCount();
  }
}

function stopCount() {
  clearTimeout(timeout);
  timer_on = 0;
}
</script>
Try it Yourself »

Browser Support

clearTimeout() is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.