We're using jquery-ajaxq to prevent specific requests from running at the same time. It works well except that our client's network sucks. We frequently see timeouts or errors with an HTTP status code of 0 which seems to indicate a connection failure or request cancellation. On those specific occasions when the status code is 0, we'd like to retry x number of times before giving up and displaying a error message to the user.
There are great Stack Overflow answers that show how to do this using $.ajax(this)
but those won't work here because we're using ajaxq.
Here's an outline of how our code works now:
doAjax = function(incomingOpts) {
// do some option defaulting here
//...
var options = {
//...
success: function(data, textStatus, jqXHR) {
// basically, if we get here, we could still have an app
// level error so we check for that and call the appropriate
// callback
if ( blah blah ) {
incomingOpts.handleSuccess(data);
else
incomingOpts.handleError(data);
},
error: function(jqXHR, textStatus, errorThrown) {
// throw up a big error dialog to the user with
// as much data as we can include about what happened
//...
}
};
_.extend(options, incomingOpts);
return $.ajaxq(options.url.substring(0, 30), options);
};
It's easy enough in the error method to identify the condition where we'd want to retry in the error function above:
if ( textStatus === "error" && errorThrown === "" && jqXHR.status === "0" ) {
// then we want to retry
}
What we're having trouble with is how do implement the retry logic.. Also, we'd need to make sure only one call in the stack gets to do the logging of the error if all of them fail and make sure only one calls the appropriate success function if one of the retries actually works.