i need to create multiple javascript functions which have a static id inside, so the function itself knows what data to process.
Here is some code:
(function(){
function log(s){
if(console && console.log) console.log(s);
else alert(s);
}
var i = 10; while (i--){
window.setTimeout(function(){
// i need i to be 10, 9, 8... here not -1
log(i);
},500);
}
})();
The problem ist that i allways gets updated by the loop, and i need to prevent this.
Thanks in advance for any help, comments or tips!
A little better approach to using an immediately invoked function in each iteration, is to have your
log()function return a function.The overall result is that you end up constructing fewer function objects.
If you wanted the calls to be at an interval, either use
setInterval(), or change this:to this: