我知道问题听起来很奇怪,但问题就在这里,以下代码完美运行。计时器从 30 分钟开始,每秒钟未检测到鼠标移动都会倒计时。当检测到鼠标移动时,计时器重置为 30 分钟,在页面不活动的 25 分钟标记处,一个 CSS 弹出窗口显示倒计时最后 5 分钟,在 30 分钟时,用户自动注销。但是,如果用户打开了页面但正在完全查看另一个网页,则计时器会减慢或完全停止,具体取决于浏览器。这实际上完全否定了脚本。是否可以让脚本继续正常倒计时,并且即使用户没有主动查看页面,仍然强制用户离开页面?或者这些浏览器怪癖是为了减少内存负载?
var Timing = 0;
var CounterTime = 0;
var TimePast = 0;
var Seconds = 1800;
var Warn = 1500;
var MinuteLeft = 30;
var SecondLeft = 60;
var StopRefresh = 0;
function ResponseTime()
{
Timing = Timing + 100;
CounterTime = CounterTime + 100;
if(Timing % 1000 == 0)
{
TimePast = TimePast + 1;
SecondLeft = SecondLeft - 1;
if(SecondLeft == 59)
{
MinuteLeft = MinuteLeft-1;
}
if(SecondLeft == 0)
{
SecondLeft = 60;
}
}
if(MinuteLeft != 0)
{
if(SecondLeft == 60)
{
document.getElementById('CountdownTimer').firstChild.nodeValue = MinuteLeft+":00";
}else if(SecondLeft < 10)
{
document.getElementById('CountdownTimer').firstChild.nodeValue = MinuteLeft+":0"+SecondLeft;
}else
{
document.getElementById('CountdownTimer').firstChild.nodeValue = MinuteLeft+":"+SecondLeft;
}
if((MinuteLeft == 0) && (SecondLeft <= 10))
{
document.getElementById('CountdownTimer').style.fontWeight = "bolder";
document.getElementById('CountdownTimer').style.color = "red";
}
document.getElementById('CountdownTimer').style.fontWeight = "normal";
document.getElementById('CountdownTimer').style.color = "black";
}else
{
document.getElementById('CountdownTimer').firstChild.nodeValue = SecondLeft;
if((MinuteLeft == 0) && (SecondLeft <= 10))
{
document.getElementById('CountdownTimer').style.fontWeight = "bolder";
document.getElementById('CountdownTimer').style.color = "red";
}else
{
document.getElementById('CountdownTimer').style.fontWeight = "normal";
document.getElementById('CountdownTimer').style.color = "black";
}
}
if(TimePast == 1800)
{
document.getElementById('DoLogoutRequest').submit();
}
if(MinuteLeft <=4)
{
document.getElementById('Overlay').style.visibility="visible";
document.getElementById('ForceLogout').style.visibility="visible";
}else
{
document.getElementById('Overlay').style.visibility="hidden";
document.getElementById('ForceLogout').style.visibility="hidden";
}
$(document).ready(function(){
$(document).mousemove(function(){
Timing = 0;
TimePast = 0;
SecondLeft = 60;
MinuteLeft = 29;
});
});
}