Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax request cancelled slows the current request

I'm currently using a tool to retrieve addresses in a database on the jQuery event keypress. When the text input loses focus, I cancel every pending requests to avoid the dropdown appears after the user finished to fill in the input. Everything works correctly, but, to finish I Send a final ajax request to go to the next step of my form. This request is really much slower than before I cancelled all these requests. I don't understand why, a cancelled request shouldn't affect the pending one (and I'm sure they are cancelled, looking it in the network tab in chrome tools). I'm using this code :

jQuery.xhrPool = [];
jQuery.xhrPool.abortAll = function() {
    jQuery(this).each(function(idx, jqXHR) {
        jqXHR.abort();
        jQuery('.loader').hide();
    });
};

jQuery.ajaxSetup({
    beforeSend: function(jqXHR) {
        jQuery.xhrPool.push(jqXHR);
    },
    complete: function(jqXHR) {
        var index = jQuery.xhrPool.indexOf(jqXHR);
        if (index > -1) {
            jQuery.xhrPool.splice(index, 1);
        }
    }
});

jQuery('#my_input').blur(function(){
    jQuery.xhrPool.abortAll();
});

I guess there i an optimization trick I don't get. Thanks for you help.

like image 843
M4nch4k Avatar asked Aug 23 '13 14:08

M4nch4k


1 Answers

Remember too that opening an AJAX request to the server, and then canceling the request will not cancel the request from processing on the server, it simply stops the browser from listening for a response. The server will continue to process any disconnected ajax requests until they finish running, at which point it tries to send a response to the client and finding no one listening, dies.

like image 88
Mark Avatar answered Sep 30 '22 16:09

Mark