Last active
January 27, 2017 01:41
-
-
Save 1franck/4fa68facc9bdce2a880fcacf5f4c2b90 to your computer and use it in GitHub Desktop.
Check if an image is loaded after x attempt(s) of x ms each
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
function ImageLoader(url, callback, attempt_count, attempt_interval) { | |
var $this = this, | |
callback = callback, | |
src = url, | |
attempt = 0, | |
attempt_count = attempt_count || 5, | |
attempt_interval = attempt_interval || 500, | |
loaded = false, | |
intervalInstance; | |
var img = new Image(); | |
img.onload = function() { | |
loaded = true; | |
} | |
img.src = src; | |
var intervalInstance = setInterval(function() { | |
if(attempt_count < attempt) { | |
++attempt; | |
} | |
else { | |
clearInterval(intervalInstance); | |
callback(loaded, img); | |
} | |
}, attempt_interval); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use: