window.clearTimeout Method Javascript DOM Share / Like
The clearTimeout method of the window object is used to stop a timeout based function from running. setTimeout() initiates the timed functionality, while clearTimeout() stops it.
Parameters
timer
The identifier that represents the setTimeout() that you wish to stop.
Javascript CODE EXAMPLE
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function countDown(secs) {
var btn = document.getElementById('btn');
btn.value = "Wait for "+secs+" seconds";
if(secs < 1) {
window.clearTimeout(timer);
btn.disabled = false;
btn.value = 'OK click here now';
}
secs--;
var timer = window.setTimeout('countDown('+secs+')',1000);
}
</script>
</head>
<body>
<input disabled type="submit" id="btn" value="Wait for 5 seconds">
<script type="text/javascript">countDown(5);</script>
</body>
</html>
TIP: You can choose to not apply the reference for the "window" object since it is implied if not specified. window.clearTimeout(timer); works the same as clearTimeout(timer);