我有以下dojo小部件:
define("myWidgets/TimeoutAlert", [
"dojo/_base/declare",
"dojo/dom-style",
"dojo/request",
"dijit/ConfirmDialog",
"dijit/_WidgetBase"
], function(declare, domStyle, request, ConfirmDialog,
_WidgetBase) {
return declare([_WidgetBase], {
extendTimeout: function() {
var self = this;
var xhrArgs = {
url: "rest/data/myEndPoint",
headers: {
"Content-Type": "application/json; charset=utf-8"
},
handleAs: "json"
};
request.get(xhrArgs.url, xhrArgs).then(
function () {
console.log("Extended Timeout. Restart Check.");
self.checkForTimeout();
// broadcast the same to other windows
localStorage.setItem("myBroadcast", "extendTimeout");
});
},
showTimeoutWarning: function() {
var self = this;
var myDialog = new ConfirmDialog({
title: 'Warning',
content: 'This is a warning. Press OK now!',
closable: false
});
domStyle.set(myDialog.cancelButton.domNode, "display", "none");
// register events
myDialog.on("execute", function() {
self.extendTimeout();
});
// show warning
myDialog.show();
},
checkForTimeout: function () {
var self = this;
setTimeout(function() {
self.showTimeoutWarning();
}, 10 * 1000);
},
receiveMessage: function(ev) {
var self = this;
if(ev.key === "myBroadcast") {
var data = ev.newValue;
if (data === "extendTimeout") {
console.log('Extending session on other window now!');
window.SessionTimeoutAlert.extendTimeout();
}
}
localStorage.removeItem("myBroadcast");
},
startCheckingTimeout: function () {
var self = this;
localStorage.removeItem("myBroadcast");
window.addEventListener("storage", self.receiveMessage);
window.TimeoutAlert = self;
this.checkForTimeout();
}
});
});
它所做的只是在10秒钟的超时时间过后显示ConfirmDialog
。当用户在OK
上按下ConfirmDialog
时,将执行一次休息呼叫,并且timeout -> ConfirmDialog -> press OK -> rest call
的相同周期将不断重复。
现在,假设小部件在两个窗口/选项卡上并行运行,并且ConfirmDialog
同时出现在两个窗口上。
我正在尝试使用localStorage
和window storage event listener
将一个窗口中的OK(确定)按钮传达给所有其他窗口。
这在Chrome和Firefox中正常运行。
但是,在IE中,extendTimeout
甚至会为执行以下操作的窗口再次触发:
localStorage.setItem("myBroadcast", "extendTimeout");
这导致rest调用执行两次,因此弹出窗口在每个周期中保持平方。
为什么会这样?
在这里我可以做些什么来阻止这种情况?
为什么调用receiveMessage
的窗口将数据存储在localStorage
中?