Quantcast
Channel: JavaScript: Add delay to a jquery ajax retry - Stack Overflow
Viewing all articles
Browse latest Browse all 2

JavaScript: Add delay to a jquery ajax retry

$
0
0

I've got code in the following structure. The intent is to send an Ajax request, and to retry it some number of times with a delay between each try:

$.ajax({
  type: 'GET',
  url: 'https://example.com',
  retryLimit: 3,
  attempt: 1,
  success: function (data) {
    doStuff();
  },
  error: function (data) {
    if (this.attempt++ <= this.retryLimit) {
      var retry = function () {
        $.ajax(this);
      };
      setTimeout(retry, 1000);
    } else {
      handleError();
    }
  }
});

The problem is that I'm getting a TypeError: Illegal invocation when trying to call $.ajax. I also attempted to replace with promises, like this:

new Promise(function(resolve) {
  setTimeout(resolve, 1000);
}).then(function() {
  $.ajax(this);
});

But I got the same error. What is going on, and how can I resolve it?

NOTE: I'm using the Atlassian SDK to build a Jira plugin, so my javascript version seems to be limited. I'm not 100% sure which version I have, but I know it fails to compile when I try to use arrow functions.


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>