Window setInterval() Method - Using clearInterval() to stop time - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window setInterval

Description

Window setInterval() Method - Using clearInterval() to stop time

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="stopColor()">Stop Toggling</button>

<script>
var myVar = setInterval(function(){ setColor() }, 300);

function setColor() {//  w  w  w.j  av a 2  s  .c  o m
  var x = document.body;
  x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}

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

</body>
</html>

Related Tutorials