I'm making a game in javascript, it's kind of the game genius . 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 it for 3 times, for example instead of changing the color of a div and returning to normal color, then changing the color of another div and returning to normal, is changing the color of all divs at the same time, as if settimeout were not waiting for 1000 milliseconds.