5

When XMLHttpRequest gets error 0 it means there is no connection and most of the times you just want to retry in some time.

Is there a simple way to transparently retry the XMLHttpRequest notifying the user the connection is lost (just like gmail does)?

(I am using jQuery)

skaffman
  • 398,947
  • 96
  • 818
  • 769
Eduardo
  • 2,327
  • 5
  • 26
  • 43
  • Outside of putting your connection code into its own function and calling settimeout after changing some part of the DOM to let the user know of the connection being down? (I'd put this as an answer but I feel like I'm missing something) – Platinum Azure Jan 30 '10 at 22:35
  • possible duplicate of [How do I resend a failed ajax request?](http://stackoverflow.com/questions/8881614/how-do-i-resend-a-failed-ajax-request) – user2284570 Oct 10 '14 at 19:20

1 Answers1

11

Look here for what you're after: Defensive AJAX and AJAX retries in jQuery.

Basically in the error handler, you can do $.ajax(this);

$.ajax({
  url : 'timeOutPage.html',
  type : 'get',
  timeout : 20000,
  error : function(xhr, textStatus, errorThrown ) {
     if (textStatus == 'timeout') {
       $.ajax(this);
       return;
     }
  }
 });

The linked article has a more in-depth example with a retry limit, etc...as with most jQuery, you can almost copy/paste their example to get going.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155