在两种颜色之间切换不起作用

switching between two colors is not working

我在这里缺少什么,所以我的页面在这两种颜色之间切换?谢谢 !

var body = document.querySelector("body");
var isBlue = false;

setInterval(function(){
    if(isBlue){
        body.style.background = "green";
    } else {
        body.style.background = "white"
    }
},1000);

你永远不会改变 isBlue 的值,所以它总是 false,所以你总是将 white 设置为背景颜色。

if/else 之后,反转标志:

isBlue = !isBlue;

例如:

setInterval(function(){
    if(isBlue){
        body.style.background = "green";
    } else {
        body.style.background = "white"
    }
    isBlue = !isBlue;
},1000);

旁注:"isBlue" 对于设置 绿色 背景的标志来说似乎是一个奇怪的名字...;-)(尽管公平,据我所知在某些文化中,蓝色和绿色之间没有区别。)