Javascript Browser Window setInterval() Method stop by clearInterval()

Introduction

Using clearInterval() to stop time:

View in separate window

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<button onclick="myStopFunction()">Stop time</button>
<script>
var myVar = setInterval(myTimer, 1000);

function myTimer() {/*from   ww  w.  ja  v a 2 s  .  c  o  m*/
  var d = new Date();
  var t = d.toLocaleTimeString();
  document.getElementById("demo").innerHTML = t;
}

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

</body>
</html>



PreviousNext

Related