Javascript Browser Window clearInterval() Method stop a timer

Description

Javascript Browser Window clearInterval() Method stop a timer

View in separate window

<!DOCTYPE html>
<html>
<body>

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

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

function setColor() {/*  w w w  .ja  va2s  . co m*/
  var x = document.body;
  x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}

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

</body>
</html>



PreviousNext

Related