<html>
<body>
<p>In this example, the setInterval() method executes the setColor() function once every 300 milliseconds, which will toggle between two background colors.</p>
<button onclick="stopColor()">Stop Toggling</button>
<script>
var myVar = setInterval(setColor, 300);
function setColor() {
var x = document.body;
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
function stopColor() {
clearInterval(myVar);
}
</script>
</body>
<!-- Mirrored from www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_setinterval_clearinterval2 by HTTrack Website Copier/3.x [XR&CO'2014], Mon, 27 Jan 2020 03:04:10 GMT -->
</html>