Javascript Browser Window clearTimeout() Method

Introduction

Prevent the function set with the setTimeout() to execute:

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>
<button onclick="myStopFunction()">Stop the alert</button>

<script>
var myVar;//  w  w w .ja v  a 2 s .  c  o m

function myFunction() {
  myVar = setTimeout(function(){ alert("Hello"); }, 3000);
}

function myStopFunction() {
  clearTimeout(myVar);
}
</script>

</body>
</html>

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

The ID value returned by setTimeout() is used as the parameter for the clearTimeout() method.

clearTimeout(id_of_settimeout);

Parameter Values

ParameterDescription
id_of_settimeout Required. The ID value of the timer returned by the setTimeout() method

The clearTimeout() method has No return value.




PreviousNext

Related