javascriptjqueryhtmlajaxqueue

Get latest ajax request and abort others


I have been searching and this problem seems simple but cannot find answer. I have multiple request calling different url. But for each url, I only want the result once and it must be the last one in the same url being called. My issue now is "how to get the last one only?" I looked at this and it seems to be 3 years old:

http://plugins.jquery.com/project/ajaxqueue

Any other way to do this nicely and cleanly? If there is something like this, it would be perfect:

queue: "getuserprofile",
cancelExisting: true

(where the existing ajax in getuserprofile queue will be canceled)

Thanks


Solution

  • 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.
    }