The clearTimeout()
method clears a timer set with the setTimeout() method.
clearTimeout |
Yes | Yes | Yes | Yes | Yes |
clearTimeout(id_of_settimeout)
Parameter | Description |
---|---|
id_of_settimeout | Required. The timer ID returned by the setTimeout() method |
No return value.
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: