Window clearInterval() Method - Toggle between two background colors once every 300 milliseconds, until it is stopped by clearInterval(): - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window clearInterval

Description

Window clearInterval() Method - Toggle between two background colors once every 300 milliseconds, until it is stopped by clearInterval():

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  a  va2  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