Last active
August 29, 2015 14:12
-
-
Save kjbrum/4fb28bc97a492c966dfe to your computer and use it in GitHub Desktop.
Run something after images from ajax are loaded.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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