Toggle between two background colors once every 300 milliseconds:
<!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>