Possible Duplicate:
Javascript closure inside loops - simple practical example
I have a loop with an image.onload call like so:
for (var i = 0; i < array.length; i++){
var item = array[i]
, image = new Image();
image.onload = function(){
// do something with 'item'
}
image.src = url;
}
Because I do not know when the onload function is being triggered, I believe that item is getting overwritten. How do I make sure that each onload function is triggered with the item variable referencing the data that it does at the time the function is bound to the onload event?