Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active August 29, 2015 14:12
Show Gist options
  • Save kjbrum/4fb28bc97a492c966dfe to your computer and use it in GitHub Desktop.
Save kjbrum/4fb28bc97a492c966dfe to your computer and use it in GitHub Desktop.
Run something after images from ajax are loaded.
$(document).ready(function() {
// Check for all images to be loaded
$.fn.imagesLoaded = function() {
var dfds = [];
var $imgs = this.find('img[src!=""]');
$imgs.each(function() {
var dfd = $.Deferred();
dfds.push(dfd);
var img = new Image();
img.onload = function() {
dfd.resolve();
}
img.src = this.src;
});
return $.when.apply($,dfds);
}
});
// Use case
function someFunction(variable){
$.ajax({
cache: false,
dataType : 'json',
url: wp_info.home_url + '/wp-admin/admin-ajax.php',
type: 'POST',
data: {
action: 'function_in_functions_php',
variable: variable
},
error: function(response, textStatus, errorThrown) {
console.log(response.error);
},
success: function(response, textStatus, jqXHR) {
if (response.status == 'success') {
$('.div').html(response.html).imagesLoaded().then(function() {
somethingHere();
});
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment