Skip to content

Instantly share code, notes, and snippets.

@basgys
Last active December 10, 2015 13:48
Show Gist options
  • Save basgys/4443808 to your computer and use it in GitHub Desktop.
Save basgys/4443808 to your computer and use it in GitHub Desktop.
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