js切换背景颜色

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>

<button onclick="stopColor()">停止切换</button>

<script>
var myVar = setInterval(function(){ setColor() }, 50);

function setColor() {
var x = document.body;
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}

function stopColor() {
clearInterval(myVar);
}
</script>

</body>
</html>

posted @ 2019-08-09 18:09  钧一  阅读(4392)  评论(0编辑  收藏  举报