So I'm trying to replace a bunch of images through ajax call, but the same image is loaded multiple times in some instances, which is caused by the asynchronous nature of the ajax call. (the success function being called not sequentially) I realize that the jQuery.ajax function does have a parameter for async: false, but the page that I'm loading with the ajax call is somewhat large and would have terrible load time if loaded synchronously. Is there a way use async still but at the same time ensure that each image will be loaded correctly?
jQuery(document).ready(function($) {
var thumb = $('.ihPhotoThumb'); //original image elements
for(var i = 0; i < thumb.size(); i++){ //go through all the images
replaceImage($('.ihPhotoThumb').eq(i)); //call this function to replace the images
}
});
function replaceImage(elem1){
jQuery.ajax({
url: elem1.parent().attr('href'),
success: function(data){
var image_url =(jQuery(data).find("#ihf_detail_mainphoto_lrg img").attr('src')); //get the image url, which is wrapped in "#ihf_detail_mainphoto_lrg img"
elem1.attr('src', image_url); //replace the picture
}
});
}
The page in question is located here: http://www.idxre.com/toppicks/52813/OffBeachUnder3m/71472 The script starts at line 1714.