views:

333

answers:

1

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)

+2  A: 

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