jerkovicl
8/14/2014 - 1:17 PM

Retrying a jQuery.ajax() call

Retrying a jQuery.ajax() call

$.ajax({
  url: '/echo/error/',
  async: true,
  // retryCount and retryLimit will let you retry a determined number of times
  retryCount: 0,
  retryLimit: 10,
  // retryTimeout limits the total time retrying (in milliseconds)
  retryTimeout: 10000,
  // timeout for each request
  timeout: 1000,
  // created tells when this request was created
  created : Date.now(),
  error : function(xhr, textStatus, errorThrown ) {
    this.retryCount++;
    if (this.retryCount <= this.retryLimit && Date.now() - this.created <  this.retryTimeout) {
      console.log("Retrying");
      $.ajax(this);
      return;
    }
  }
});​