What is it that I am missing here so my page switches between these two colors? Thank you !
JavaScript
x
11
11
1
var body = document.querySelector("body");
2
var isBlue = false;
3
4
setInterval(function(){
5
if(isBlue){
6
body.style.background = "green";
7
} else {
8
body.style.background = "white"
9
}
10
},1000);
11
Advertisement
Answer
You’re never changing the value of isBlue
, so it’s always false
, so you always set white
as the background color.
Afrer the if
/else
, invert the flag:
JavaScript
1
2
1
isBlue = !isBlue;
2
E.g.:
JavaScript
1
9
1
setInterval(function(){
2
if(isBlue){
3
body.style.background = "green";
4
} else {
5
body.style.background = "white"
6
}
7
isBlue = !isBlue;
8
},1000);
9
Side note: “isBlue” seems an odd name for a flag that sets a green background… 😉 (Though to be fair, as I understand it in some cultures there isn’t a distinction between blue and green.)