I have code in this structure.
If the error status is 429, I want to retry after 3 seconds, a maximum of 3 times.
$.ajax({
url : 'url',
type : 'PUT',
data : 'data',
tryCount : 0,
retryLimit : 3,
success : function() {
// do something
},
error : function(response, status, error ) {
if (response.status == 429) {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
$.ajax(this);
return;
} else {
console.log('update failed!');
}
} else {
// handle other errors
}
}
});
Where do I add the timeout? I keep getting an endless loop.
2
Answers
You just need to replace
$.ajax(this)
with asetTimeout
wrapper around it:have you considered putting your $.ajax in function?
adding a counter variable combined with a setTimeout could do the trick.