Javascript examples for Browser Object Model:Window setInterval
Window setInterval() Method - Using clearInterval() to stop time
<!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>