运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
转到 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>