I'm trying to write code that loads multiple images with ajax once a thumbnail is clicked. The load method is wrapped in a for loop, since the images are named my-image-1, my-image-2 etc.. I would like to load the images by increasing i, and exit the loop once /my-image-i.jpg doesn't exist, yielding an error.
function load_slides(i) {
console.log(i);
var url = "some-url.jpg";
var img = $('<img />').attr('src', url).load(function(response, status, xhr) {
if (status == "error") {
console.log("Error loading");
return false;
}
});
}
//code below is triggered upon clicking the thumbnail
for (var i = 1;; i++) {
if (!load_slides(i)) break;
else {
load_slides(i);
}
}
With the code above, I never see the error message and the loop never stops executing, when it shouldn't even start with a bad url :/
Thanks in advance for your help, I've been trying to figure this out for a while