pattern.forEach( function(elem, index) {
setTimeout(yo(elem),index*1000);
function yo(elem){
console.log(elem);
switch (elem) {
case 'blue':
// statements_1
$('#blue').css('background-color','grey');
bsound.play();
setTimeout(function(){$('#blue').css('background-color','blue');},500);
break;
case 'green':
// statements_1
$('#green').css('background-color','grey');
gsound.play();
setTimeout(function(){$('#green').css('background-color','green');},500);
break;
case 'red':
// statements_1
$('#red').css('background-color','grey');
bsound.play();
setTimeout(function(){$('#red').css('background-color','red');},500);
break;
case 'yellow':
// statements_1
$('#yellow').css('background-color','grey');
ysound.play();
setTimeout(function(){$('#yellow').css('background-color','yellow');},500);
break;
}
}
});
Hey, guys, I am trying to build a Simon game. Now as the game follow the computer will generate random patterns. Every color has its specific sound which will be played. Now the logic I am using here is we will loop on pattern array using foreach method. At every element, I will call settimeout callback function with index*1000. I am multiplying index with ms so there is a gap between every call. Now if I have a pattern array of say [‘red’] it works. for [‘red’,‘yellow’] It will simultaneously play both blocks sound for yellow and red and will also change the color of those blocks to gray and changing it back to their original color (problem is it is doing that simultaneously). if [‘red’,‘yellow’,‘red’] it will do the same thing as for [‘red’,‘yellow’] (meaning it skips the red color sound). Can any body please tell me why this is happening.I searched stackoverflow and people suggested to use index*ms to get the desired delay.....