Created
February 3, 2015 18:07
-
-
Save matheuscl/147caf92e1ea1375a84e to your computer and use it in GitHub Desktop.
This function help when u need to add img after page load...
This file contains 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
$.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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool