Javascript Browser Window setInterval() Method toggle background

Introduction

Toggle between two background colors once every 300 milliseconds:

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="stopColor()">Stop Toggling</button>
<script>
var myVar = setInterval(setColor, 300);

function setColor() {/*from www . j  a  va  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>



PreviousNext

Related