Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
浏览器在“以后”继续执行Javascript;“页面卸载”&;新";“页面加载”;_Javascript_Ajax_Google Chrome_Firefox_Internet Explorer 11 - Fatal编程技术网

浏览器在“以后”继续执行Javascript;“页面卸载”&;新";“页面加载”;

浏览器在“以后”继续执行Javascript;“页面卸载”&;新";“页面加载”;,javascript,ajax,google-chrome,firefox,internet-explorer-11,Javascript,Ajax,Google Chrome,Firefox,Internet Explorer 11,我们有以下AJAX节流器。这是为了能够对一个页面执行多个(20+个)ajax请求,而不会因为第一个X请求总共花费了60秒而导致剩余的超时 RequestThrottler: { maximumConcurrentRequests: 3, //default to 3 requestQueue: new Array(), numberOfRequestCurrentlyProcessing: 0, addRequestToQueue: functi

我们有以下AJAX节流器。这是为了能够对一个页面执行多个(20+个)ajax请求,而不会因为第一个X请求总共花费了60秒而导致剩余的超时

RequestThrottler: {
    maximumConcurrentRequests: 3, //default to 3        
    requestQueue: new Array(),
    numberOfRequestCurrentlyProcessing: 0,

    addRequestToQueue: function (currentRequest) {
        var self = this;
        self.requestQueue.push(currentRequest);

        if (self.numberOfRequestCurrentlyProcessing < self.maximumConcurrentRequests) { self.sendNextRequest(); }
    },

    sendNextRequest: function () {
        var self = this;
        if (self.numberOfRequestCurrentlyProcessing >= self.maximumConcurrentRequests) { return; }
        if (self.requestQueue.length === 0) { return; }

        var currentRequest = self.requestQueue.pop();
        self.numberOfRequestCurrentlyProcessing++;
        AJAX.SendAjaxRequest(currentRequest.url, currentRequest.httpMethod, 
            function(data){
                self.numberOfRequestCurrentlyProcessing--;
                currentRequest.onSuccessCallback(data);
                self.sendNextRequest();
            }, 
            function(){
                self.numberOfRequestCurrentlyProcessing--;
                currentRequest.onErrorCallback();
                self.sendNextRequest();
            });
    },

    sendUpdateRequest: function (currentRequest) {
        var self = this;
        self.addRequestToQueue(currentRequest);
    }
}
RequestThrottler:{
maximumConcurrentRequests:3,//默认值为3
requestQueue:新数组(),
numberOfRequestCurrentlyProcessing:0,
addRequestToQueue:函数(currentRequest){
var self=这个;
self.requestQueue.push(currentRequest);
if(self.numberOfRequestCurrentlyProcessing=self.maximumConcurrentRequests){return;}
如果(self.requestQueue.length==0){return;}
var currentRequest=self.requestQueue.pop();
self.numberOfRequestCurrentlyProcessing++;
SendAjaxRequest(currentRequest.url,currentRequest.httpMethod,
功能(数据){
self.numberOfRequestCurrentlyProcessing--;
currentRequest.onSuccessCallback(数据);
self.sendNextRequest();
}, 
函数(){
self.numberOfRequestCurrentlyProcessing--;
currentRequest.onErrorCallback();
self.sendNextRequest();
});
},
sendUpdateRequest:函数(currentRequest){
var self=这个;
self.addRequestToQueue(currentRequest);
}
}

但是,由于这些请求位于Javascript队列中,因此当用户尝试加载新页面时,开发人员工具会在新页面的网络区域显示响应。出于隐私原因,我们的应用程序有一个检查点,不允许这种行为。这对于浏览器来说是正常的,还是某种错误,还是我做错了什么?

一个干净的解决方案是监听
窗口。onbeforeunload
事件中止任何尚未收到响应的ajax请求

出于以下原因,应使用
beforeunload
事件,而不是
unload

1)
beforeunload
事件比
unload
事件更可靠:

卸载事件的具体处理因版本而异 浏览器版本。例如,Firefox的某些版本会触发 事件,但不是在窗口关闭时。在里面 实际使用情况、行为应在所有支持的浏览器上进行测试, 并与专有的beforeunload事件进行对比

资料来源:

2) 可以取消
beforeunload
事件,而不能取消
unload
事件。如果希望在发生beforeunload事件时提示用户,这将为您提供灵活性。确认将询问用户是否希望继续导航到另一个页面,或者是否希望取消,因为并非所有ajax请求都已完成

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";

  (e || window.event).returnValue = confirmationMessage;     // Gecko and Trident
  return confirmationMessage;                                // Gecko and WebKit
});
资料来源:


为什么选择onbeforeunload而不是onunload?我应该注意,我有另一个绑定到before unload事件的函数,并且注意到它在发生其他事件时触发(例如,文件下载和flash player启动)在这种情况下,我不希望我的ajax请求退出。为了防止文件下载触发PreforeUnload事件,我找到了这个解决方案:我找不到任何提及flash player的内容导致事件触发。一种可能是查看传递给beforeunload回调的evt对象,查看是否有任何东西指定它来自flash player,并相应地执行一些操作。对于flash player,简单地监视beforeunload不会导致任何问题。您之前提到过,它正在触发beforeunload事件。由于flash播放器非常具体,我觉得它脱离了原始问题的主题,您能为这个问题创建另一个问题吗?