So I have this piece of code...
var string = 'qwe';
document.addEventListener('click', function(e){
function bar(b){
var a = string[b];
if (a == 'q') {
console.log('first');
}
if (a == 'w') {
console.log('second');
}
if (a == 'e') {
console.log('third');
}
}
setTimeout( bar(0), 1000 );
});
Problem is setTimeout doesn't work. Code executes right after clicking.
It's weird because if I avoid using the closure, it works...
setTimeout(function bar(){
var a = string[0];
//...everything else
},1000 );
But that would probably make the code messy/redundant, since I plan on doing it 3 times. Ideally the working code would be...
setTimeout( bar(0), 1000 );
setTimeout( bar(1), 2000 );
setTimeout( bar(2), 3000 );
But again, setting timeOut like this doesn't work for some reason :/ Any ideas why?