Javascript Browser Window clearInterval() Method

Introduction

Display the current time.

The setInterval() method will execute the "myTimer" function once every 1 second.

Use clearInterval() to stop the time:

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<button onclick="myStopFunction()">Stop time</button>
<script>
var myVar = setInterval(myTimer, 1000);

function myTimer() {/*  w  w w.j a va2 s. co  m*/
  var d = new Date();
  var t = d.toLocaleTimeString();
  document.getElementById("demo").innerHTML = t;
}

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

</body>
</html>

The clearInterval() method clears a timer set with the setInterval() method.

The ID value returned by setInterval() is used as the parameter for the clearInterval() method.

clearInterval(var); 

Parameter Values

Parameter Description
var Required. The name of the timer returned by the setInterval() method

The clearInterval() method has No return value.




PreviousNext

Related