【发布时间】:2015-08-03 04:55:49
【问题描述】:
我在Layout(MVC4.0) (With 1 Second Interval) 页面上有一个计时器,当网站只有 1 页时它工作正常(在一个标签中 strong>) 已打开
var timeOutMinutes = 10;
var timeOutSeconds = timeOutMinutes * 60;
localStorage.setItem("SessionCounter", timeOutSeconds);
var popUpShown = false;
var logOutCalled = false;
$(document).ready(function () {
setInterval(TimerDecrement, 1000);
});
function TimerDecrement() {
if (timeOutSeconds > 0) {
if (parseInt(localStorage.getItem("SessionCounter"), 10) > 0) {
timeOutSeconds = parseInt(localStorage.getItem("SessionCounter"), 10);
}
timeOutSeconds--;
localStorage.setItem("SessionCounter", timeOutSeconds);
}
else {
if (!logOutCalled) {
logOutCalled = true;
LogOut();
}
else {
logOutCalled = true;
}
}
document.getElementById("seconds").innerHTML = timeOutSeconds;
document.getElementById("secondsIdle").innerHTML = timeOutSeconds;
if (timeOutSeconds < 500) {
//alert(history.length);
popUpShown = true;
$("#pnlPopup").dialog("open");
}
else {
if ($("#pnlPopup").dialog("isOpen")) {
popUpShown = false;
$("#pnlPopup").dialog("close");
}
}
}
但是当我打开网站的多个标签时,计时器会迅速减少。
即使在多个标签页中打开网站,我如何保持计时器均匀递减?
【问题讨论】:
-
如果您使用 sessionStorage 来存储初始时间计数,那么这个问题(从多个选项卡快速递减)将会上升,因为 sessioStorage 对于所有选项卡都是通用的。您可以使用一个简单的 javascript 变量。该变量的范围对于所有选项卡都会有所不同,并且计时器将正常工作。
-
我已经尝试过你的建议..但我希望计时器在所有选项卡上统一递减(如果打开多个)。因为当计时器归零'0'时,我必须做一些ajax调用......
-
只存储“倒计时开始”。然后可以根据当前时间计算此后的秒数。这也将确保“一致”(基于 any 选项卡中发生的任何操作),假设要共享倒计时间隔。
-
user2864740 ..我已经编辑了代码..请看一下
-
@user2864740 ..我添加了小提琴链接..请看一下
标签: javascript jquery asp.net-mvc jquery-ui