I need to make the foreach loop on the result return by the ajax. While doing foreach I am checking against each record that image exist or not.
Image Existence Code
function imageExists(url, callback) {
var img = new Image();
img.onload = function() { callback(true); };
img.onerror = function() { callback(false); };
img.src = url;
}
For each loop
hotelImagesText = '<ul class="gallery list-unstyled cS-hidden" id="image-gallery">';
$.each(hotelImagesArr, (index, item) => {
imageExists(item, function(exists) {
if (exists) {
hotelImagesText += '<li data-thumb="'+hotelImagesArr[index]+'">
<img src="'+hotelImagesArr[index]+'"></li>';
}
});
});
hotelImagesText += '</ul>';
When I console it only gives me the above string that has ul. The string inside the imageExists does not concat.