Stop all active ajax requests in jQuery

前端 未结 16 1040
自闭症患者
自闭症患者 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:17

    I found it too easy for multiple requests.

    step1: define a variable at top of page:

      xhrPool = []; // no need to use **var**
    

    step2: set beforeSend in all ajax requests:

      $.ajax({
       ...
       beforeSend: function (jqXHR, settings) {
            xhrPool.push(jqXHR);
        },
        ...
    

    step3: use it whereever you required:

       $.each(xhrPool, function(idx, jqXHR) {
              jqXHR.abort();
        });
    

提交回复
热议问题