Javascript examples for Browser Object Model:Window setInterval
Window setInterval() Method - Toggle between two background colors once every 300 milliseconds:
<!DOCTYPE html> <html> <body> <button onclick="stopColor()">Stop Toggling</button> <script> var myVar = setInterval(function(){ setColor() }, 300); function setColor() {/*from ww w . j ava 2 s . co m*/ var x = document.body; x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow"; } function stopColor() { clearInterval(myVar); } </script> </body> </html>