Get latest ajax request and abort others

This explains how to use jQuery to do an ajax call and how to abort it. All you need to do is create an array that stores each request. You could then cancel the previous request while adding the new one.

ajaxRequests = new Array();

queueRequest = function() {
    if(ajaxRequests[ajaxRequests.length - 1]) {
        ajaxRequests[ajaxRequests.length - 1].abort();
    }

    ajaxRequests[ajaxRequests.length] = //Insert New jQuery AJAX call here.
}

Since we only want the result of the last request, this is very simple and works.

var ajax = null;
var getFoo = function() {
    if(ajax) ajax.abort();
    ajax= $.ajax({});
};
getFool();
getFool();
getFool();
getFool();
getFool();
getFool();

only the last request is executed.


Instead of using library, you can use Basic jquery Ajax method :

beforeSend:{}

For example:

xhr = jQuery.ajax({
            url: /*Your URL*/
            type: "POST",
            data: {
              //data
            },
             /* if there is a previous ajax request, then we abort it and then set xhr to null */
            beforeSend : function()    {           
                if(xhr != null) {
                    xhr.abort();
                }
            },
success:function(){}

});