Last active
December 10, 2015 13:48
-
-
Save basgys/4443808 to your computer and use it in GitHub Desktop.
Reload images on failure every x seconds
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
// Reload images on failure every x seconds | |
$("img").error(function() { | |
var path = $(this).data('src'); | |
var img = $(this); | |
// Put back mock image | |
$(img).attr('src', 'http://placehold.it/56x56'); | |
// Increment loading counter | |
var attempt = ($(img).data('attempt') || 0); | |
attempt++; | |
$(img).data('attempt', attempt); | |
// Get reload time in seconds with exponential backoff (between 1 and 120 seconds) | |
var time = Math.min(120, Math.round(Math.exp(attempt/5))); | |
// Reschedule a loading attempt | |
setTimeout(function() { | |
$(img).attr('src', path); | |
}, time*1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment