<html>
<body>
<p>在此示例中,setInterval() 方法每 300 毫秒执行一次 setColor() 函数,这将在两种背景颜色之间切换。</p>
<button onclick="stopColor()">停止切换</button>
<script>
var myVar = setInterval(setColor, 300);
function setColor() {
var x = document.body;
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
function stopColor() {
clearInterval(myVar);
}
</script>
</body>
</html>