Home > Mobile >  how can set over two background-color in js setinterval()
how can set over two background-color in js setinterval()

Time:02-20

set over two background-color in javascript setInterval()

let x = document.body; x.style.background-Color = x.style.background-Color == "yellow" ? "pink": "yellow";

CodePudding user response:

Try this:

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">&nbsp</div>

  • Related