I’m making Simon game setTimeout function is not working as expected.
n is being logged in to console and glow() is being called without any delay.
I researched it on stack and found that its some closure issue I fixed it but still not working pls help finding whats wrong with this code…
for(let i = 0; i < glows.length; i++) {
(function(n) {
setTimeout(function(){
console.log(n);
glow(glows[n]);
let audio = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
audio.volume = 0.2;
audio.play();
}, 1000);
}(i));
}
Simplified code…
<a class="btn btn-primary m-5" href="#" id="button" role="button">Button</a>
<script>
$("#button").click(function() {
for(let i = 0; i < 5; i++) {
(function(n) {
setTimeout(function(){
console.log(n);
}, 1000);
}(i));
}
});
</script>