jQuery.ajax "retry"

by James Greene

JavaScript

var jqOnError = function(xhr, textStatus, errorThrown ) {
    if (typeof this.tryCount !== "number") {
        this.tryCount = 0;
    }
    if (textStatus === 'timeout') {
        if (this.tryCount < 1) {  /* hardcoded number */
            this.tryCount++;
            
            //try again
            $.ajax(this);
            return;
        }            
        return;
    }
    if (xhr.status === 500) {
        //handle error
    } else {
        //handle error
    }
};

$.ajax({
    url : 'someurl',
    type : 'POST',
    data :  '', 
    success : function(json) {
        //do something
    },
    error : jqOnError
});