Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 691
HP. Avatar asked Nov 14 '10 09:11

HP.


People also ask

How do I abort AJAX request?

Just use ajax.abort(); } //then you make another ajax request $. ajax( //your code here );

Which method is used to cancel current AJAX?

Overview. We may use the abort() method to cancel a sent AJAX request.

How do I fire AJAX request periodically?

Use setInterval() when you want to send AJAX request at a particular interval every time and don't want to depend on the previous request is completed or not. But if you want to execute the AJAX when the previous one is completed then use the setTimeout() function.

How do you trigger a change event after AJAX call?

ajax({ url, data: data, type: "POST", dataType: "json", success: function(cbdata) { update_table(cbdata); } }); } $('#selector'). on('click', changeDate); So you can call changeData() when you need it.


1 Answers

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(){}

});
like image 98
Shubham Mathur Avatar answered Oct 19 '22 22:10

Shubham Mathur