Hi I am using ajax and json for infinite scrolling and then I create a string of html to add to my webpage and call it with jQuery's after() function.
$('.product-panel:last').after(productHTML);
Now I need to wait for all the images from my new productHTML string to load and then call another javascript function I created to do some formatting.
I tried like
$('.product-panel:last').after(productHTML).promise().done(function(){
doMoreStuff();
});
it doesn't work. Can someone help? Thanks
EDIT: after following adeneo's code this is my final result and it works flawlessly.
var productLength = $('.product-panel').length-1;
$('.product-panel:last').after(productHTML);
var images = $(".product-panel:gt("+productLength+")").find('img');
var promises = [];
images.each(function(idx, img) {
var def = $.Deferred();
img.onload = def.resolve;
img.onerror = def.reject;
if ( img.complete ) def.resolve();
promises.push(def.promise());
});
$.when.apply($, promises).done(function() {
productHeight();
});
It's not quite that easy, you'll have to find all the inserted images and wait for them to load individually, something like this
var images = $('.product-panel:last').after(productHTML).next().find('img');
var promises = [];
images.each(function(idx, img) {
var def = $.Deferred();
img.onload = def.resolve;
img.onerror = def.reject;
if ( img.complete ) def.resolve();
promises.push(def.promise());
});
$.when.apply($, promises).done(function() {
// all images loaded
});