My objective is to create a simon game, and I am currently trying to change the color of each div as follows:
I am storing div classes randomly from 0 to 3; each iteration of the game adds a number to the array
var save =[];
I want to highlight the div purple at one second, at two seconds, bring it back to the assigned css color and repeat this until it iterates through all the save array class numbers.
at first i tried to do this:
setTimeout(function(){ $("."+save[0]).css("background-color", "purple"); },1000)
setTimeout(function(){ $("."+save[0]).css("background-color", ""); },2000)
setTimeout(function(){ $("."+save[1]).css("background-color", "purple"); },3000)
setTimeout(function(){ $("."+save[1]).css("background-color", ""); },4000)
and so on and so forth, as i add more iterations to the simon game.
I want to do this instead :
var i = 0;
function hello() {
$("." + save[i]).css("background-color", "purple");
}
function goodbye() {
$("." + save[i]).css("background-color", "");
}
var one = 1000;
var two = 2000;
while (i < save.length) {
setTimeout(hello(), one);
setTimeout(goodbye(), two);
i++;
one += 2000;
two += 2000
}
i went through several stack overflow questions, found similar ones, but nothing that helped. I can imagine there's youtube videos on creating this game, but I would like to figure out as much as I could on my own, it's just this one thing that has me scratching my head.