Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h1>The Window Object</h1> <h2>The setInterval() and clearInterval() Methods</h2> <p>In this example, the setInterval() method executes the setColor() function once every 500 milliseconds to toggle between two background colors.</p> <button onclick="stopColor()">Stop Toggling</button> <script> myInterval = setInterval(setColor, 500); function setColor() { let x = document.body; x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow"; } function stopColor() { clearInterval(myInterval); } </script> </body> </html>