Possible Duplicate:
Javascript closure inside loops - simple practical example
I have a for loop with an anonymous function inside, and in the function I want to access the current loop iteration. But for some reason instead of the loop iteration, I'm getting 4. The only other place 4 is a value is myArray.length. If I pass i as an argument, I get [object Object] out. What am I doing wrong? My code:
var width = function(){
for(var i = 0, len = myArray.length; i < len; ++i){
alert(i) //this outputs the current iteration
myArray[i].load(function(){
alert(i) //this outputs 4 (or [object Object])
});
};
};
Thanks.