I have an array of strings in Javascript that I need to use to load images in my page via AJAX. After each image is loaded, I need to perform some additional tasks such as sending a HTTP request which will actually delete the image.
This is what I have so far.
for (x in images) {
var img = new Image();
$(img)
.load(function(){
$('#image_'+images[x]).html(''); //remove loading gif
$('#image_'+images[x]).append(this);
$.post('/images/destroy_image',{id:images[x]});
})
.attr('src', '/images/get_image?id='+images[x]);
}
This code works fine if there is only 1 string in the images array.
If there is more than 1, when an image finishes loading and its load function is run, any reference to images[x] in its load function now point to the final string in the images array, when I need it to be the value it was when the loop was being run.
Eg.
images is {'12','100'}
When the first image finishes loading, the first line in its load function will be run as
$('#image_100').html('');
when it should be
$('#image_12').html('');
How can I do this?
$('<img/>')instead of thenew Image()stuff.imagesis{'12','100'}. That would result in a syntax error. I hope you mean['12','100'].