x
 
<!DOCTYPE html>
<html>
<body>
<h1>Window 对象</h1>
<h2>setInterval() 和 clearInterval() 方法</h2>
<p>在此例中,setInterval() 方法每 500 毫秒执行一次 setColor() 函数,在两种背景颜色之间切换。</p>
<button onclick="stopColor()">停止切换</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>