Skip to content

Instantly share code, notes, and snippets.

@basgys
Created August 15, 2012 10:19
Show Gist options
  • Save basgys/3358471 to your computer and use it in GitHub Desktop.
Save basgys/3358471 to your computer and use it in GitHub Desktop.
Reload image on failure with exponential backoff
$("img.thumb").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