2
I’m making a game in javascript, it’s like the game Genius.I’m using setTimeout inside the for.
function executarJogo(numeroJogada){
var cores = ["#FFFF00","#00FF00","#0000FF","#FF0000"];
for (var i = 0; i < numeroJogada; i++) {
var numQuadro = Math.floor((Math.random() * 4) + 1);
var corQuadro = Math.floor((Math.random() * 4) + 1);
var q = "c" + numQuadro;
console.log(corQuadro -1,cores[corQuadro-1]);
var quadrado = document.getElementById(q);
quadrado.style.background=cores[corQuadro-1];
doTimeOut(quadrado);
}
}
function doTimeOut (quadrado) {
setTimeout(function() {quadrado.style.background="#cdc8b1";}, 1000);
}
The problem is, when I run the 3 times for example instead of changing the color of a div and going back to the normal color, then changing the color of another div and going back to normal, is changing the color of all the Ivs at the same time, as if the settimeout wasn’t waiting for the 1000 milliseconds.