Skip to content

Instantly share code, notes, and snippets.

@basgys

basgys/gist:4443808

Last active Dec 10, 2015
Embed
What would you like to do?
Reload images on failure every x seconds
// 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
You can’t perform that action at this time.