Mahmoud Santos
Published in : 2022-02-22
How to Set two background-color in the javascript by setInterval( )?
let x = document.body;
x.style.backgroundColor = x.style.backgroundColor === "yellow" ? "pink": "yellow";
Yasen Sayed Date : 2022-02-22
Best answers
8
Best answers
8
Try this approach,
var i = 0;
function change() {
var doc = document.getElementById("background");
var color = ["black", "blue", "brown", "green"];
doc.style.backgroundColor = color[i];
i = (i + 1) % color.length;
}
setInterval(change, 1000);
<div id="background"> </div>
Join our community and get the chance to solve your code issues & share your opinion with us
Sign up Now