Skip to content

Instantly share code, notes, and snippets.

@matheuscl
Created February 3, 2015 18:07
Show Gist options
  • Save matheuscl/147caf92e1ea1375a84e to your computer and use it in GitHub Desktop.
Save matheuscl/147caf92e1ea1375a84e to your computer and use it in GitHub Desktop.
This function help when u need to add img after page load...
$.imgReload = function() {
var retries = 0;
var loaded = 1;
$("img").each(function() {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
var src = $(this).attr("src");
var date = new Date();
$(this).attr("src", src + "?v=" + date.getTime()); //slightly change url to prevent loading from cache
loaded =0;
}
});
retries +=1;
if(retries < 10){ //if after 10 retries error images are not fixed maybe because they are not present on server, the recursion will break the loop
if(loaded == 0)
{setTimeout('$.imgReload()',4000); // I think 4 seconds is enough to load a small image (<50k) from a slow server
}
//all images have been loaded
else {// alert("images loaded");
}
}
//if error images cannot be loaded after 10 retries
else {// alert("recursion exceeded");
}
}
@tyshonda
Copy link

tyshonda commented Feb 3, 2015

cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment