javascript - Why doesn't this setInterval code work? -
I am trying to change the background color of every other body but it does not work. what I have done?
var color = ["red", "blue", "green", "yellow", "red"], rand = monastery. Cecil (mathematics.rendum () * Colors.length - 1), t = setInterval (function () {document.body.style.backgroundColor = colors [rand];}, 1000);
You can use once and again again the value rand You are defining it every time you mean: var color = ["red", "blue", "green", "yellow", "red"]; T = setInterval (function () {// Use CEL floor instead of use, and then you can leave -1 var rand = Math.flur (Math.Rendam) * colors.length) document.body.style. BackgroundColor = colors [rand];}, 1000); In addition, you are using commands to separate the command, while it will not always be broken (I can think of at least one situation Where it will not be), it is usually a bad idea because it means that some big structure is going on. According to Patrick dw , you have the right to use coma. In that context, I had a mental slip, especially when the rand was moved to the function of setInterval . I can suggest that you can write that var color, T;
Comments
Post a Comment