Stop all active ajax requests in jQuery

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

    Here's what I'm currently using to accomplish that.

    $.xhrPool = [];
    $.xhrPool.abortAll = function() {
      _.each(this, function(jqXHR) {
        jqXHR.abort();
      });
    };
    $.ajaxSetup({
      beforeSend: function(jqXHR) {
        $.xhrPool.push(jqXHR);
      }
    });
    

    Note: _.each of underscore.js is present, but obviously not necessary. I'm just lazy and I don't want to change it to $.each(). 8P

提交回复
热议问题