0

当用户导航到其他链接时,我面临未定义的错误。我在页面加载时触发 6-7 jquery,它在加载时工作正常。当用户不断刷新页面或单击其他链接时,它会显示错误。我认为先前请求的此错误 bcoz 未完成。我在互联网上查看并找到了这个有用的技巧

 // Automatically cancel unfinished ajax requests 
// when the user navigates elsewhere.
(function($) {
  var xhrPool = [];
  $(document).ajaxSend(function(e, jqXHR, options){
    xhrPool.push(jqXHR);
  });
  $(document).ajaxComplete(function(e, jqXHR, options) {
    xhrPool = $.grep(xhrPool, function(x){return x!=jqXHR});
  });
  var abort = function() {
    $.each(xhrPool, function(idx, jqXHR) {
      jqXHR.abort();
    });
  };

  var oldbeforeunload = window.onbeforeunload;
  window.onbeforeunload = function() {
    var r = oldbeforeunload ? oldbeforeunload() : undefined;
    if (r == undefined) {
      // only cancel requests if there is no prompt to stay on the page
      // if there is a prompt, it will likely give the requests enough time to finish
      abort();
    }
    return r;
  }
})(jQuery);

http://stackoverflow.com/questions/1802936/stop-all-active-ajax-requests-in-jquery 它在 google chrome 和 Opera 中运行良好,但在 Internet Explorer 和 Mozilla 中无法运行。谁能告诉我如何更改此代码以支持所有浏览器。

4

0 回答 0