Skip to content

Instantly share code, notes, and snippets.

@asafge
Last active August 29, 2015 14:00
Show Gist options
  • Save asafge/11218826 to your computer and use it in GitHub Desktop.
Save asafge/11218826 to your computer and use it in GitHub Desktop.
JS - Wait for an image to load by periodically checking it's status. Useful when images are cached, and some browsers don't trigger appropriate events.
var $img = ...,
imagesLoadedCallback = function() {
// Do stuff
}
(function waitForImageLoaded() {
if ($img[0].complete && $img[0].naturalWidth > 0) {
imagesLoadedCallback();
}
else {
setTimeout(waitForImageLoaded, 300);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment