我們有以下的AJAX調速器。這被實現爲能夠對一個頁面執行許多(20+)ajax請求,而不會因爲第一個X請求總共需要60秒而超時。瀏覽器在「PageUnload」和新的「PageLoad」後繼續執行Javascript
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);
}
}
然而,因爲這些請求都坐在一個Javascript隊列中,當用戶嘗試加載一個新的頁面,開發者工具顯示在新頁面的淨面積的響應。我們的應用程序爲隱私原因進行了檢查,以防止這種行爲。這對瀏覽器來說是正常的,還是某種錯誤,或者我做錯了什麼?
爲什麼你選擇的onbeforeunload而不是onunload的? – theB3RV 2015-03-03 13:26:13
更新地址推理的答案 – peterdotjs 2015-03-03 17:06:43
我應該注意到,我有另一個綁定到before unload事件的函數,並且注意到它在其他事件發生時觸發(例如文件下載和flash播放器啓動)。在這種情況下,我不希望我的ajax請求退出。 – theB3RV 2015-03-03 20:12:17