Stop all active ajax requests in jQuery

前端 未结 16 903
自闭症患者
自闭症患者 2020-11-22 13:51

I have a problem, when submitting a form all active ajax request fail, and that triggers error event.

How to stop all active ajax requests in jQuery without trigerri

16条回答
  •  名媛妹妹
    2020-11-22 14:13

    Make a pool of all ajax request and abort them.....

    var xhrQueue = []; 
    
    $(document).ajaxSend(function(event,jqxhr,settings){
        xhrQueue.push(jqxhr); //alert(settings.url);
    });
    
    $(document).ajaxComplete(function(event,jqxhr,settings){
        var i;   
        if((i=$.inArray(jqxhr,xhrQueue)) > -1){
            xhrQueue.splice(i,1); //alert("C:"+settings.url);
        }
    });
    
    ajaxAbort = function (){  //alert("abortStart");
        var i=0;
        while(xhrQueue.length){ 
            xhrQueue[i++] .abort(); //alert(i+":"+xhrQueue[i++]);
        }
    };
    

提交回复
热议问题