for( var i=0; i<20; i++)
setTimeout(function(){
console.log(">>> "+i);
}, i*100);
So, the code above outputs >>> 19
20 times. To keep i
at it's iteration value I'm using a closure:
for(var i=0; i<20; i++)(function(i){
setTimeout(function(){
console.log(">>> "+i);
}, i*100);
}(i));
What's the problem? The problem is loop control statements, continue;
I can do with return;
but for those times when I need break;
code gets counter-intuitive when others try to read it.
So what can I do?