Javascript Reference - Window clearTimeout() Method








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

Browser Support

clearTimeout Yes Yes Yes Yes Yes

Syntax

clearTimeout(id_of_settimeout)

Parameter Values

Parameter Description
id_of_settimeout Required. The timer ID returned by the setTimeout() method




Return Value

No return value.

Example

The following code shows how to prevent the function set with the setTimeout() to execute.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<button onclick="myStopFunction()">Stop console output</button>
<script>
var myVar;
function myFunction() {<!--   ww w.j a  v  a 2s .  c om-->
    myVar = setTimeout(function(){console.log("Hello")}, 3000);
}
function myStopFunction() {
    clearTimeout(myVar);
}
</script>

</body>
</html>

The code above is rendered as follows: