Javascript 如果用户空闲一定时间,则会话超时
您好,如果用户空闲1分钟,我正在注销该用户,并且我正在尝试延长KeyPress、onmousemove、OnMouseOut等上的会话超时。会话超时正在发生,但上述事件上的会话超时没有发生。请在这方面帮助我。 我的代码:Javascript 如果用户空闲一定时间,则会话超时,javascript,session,timeout,Javascript,Session,Timeout,您好,如果用户空闲1分钟,我正在注销该用户,并且我正在尝试延长KeyPress、onmousemove、OnMouseOut等上的会话超时。会话超时正在发生,但上述事件上的会话超时没有发生。请在这方面帮助我。 我的代码: var InactivityTime = function () { var t; window.onload = resetTimer(); document.onmousemove = resetTimer(); document.onkey
var InactivityTime = function () {
var t;
window.onload = resetTimer();
document.onmousemove = resetTimer();
document.onkeypress = resetTimer();
document.onmousedown = resetTimer();
document.onmouseclick= resetTimer();
document.onmouseup=resetTimer();
function logout() {
alert("Your session has been expired.Please Re-Login to continue");
}
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 60000);
// 1000 milisec = 1 sec
}
return {
//main function to initiate the module
init: function () {
resetTimer();
}
};
}();
您需要在init函数中注册事件侦听器,如下所示:
return {
//main function to initiate the module
init: function () {
resetTimer();
window.onload = resetTimer();
document.onmousemove = resetTimer();
document.onkeypress = resetTimer();
document.onmousedown = resetTimer();
document.onmouseclick= resetTimer();
document.onmouseup=resetTimer();
}
};
此外,这些事件侦听器似乎是多余的。我会把它们简化为按键和鼠标移动
调试的一个好方法是在resetTimer()函数中添加console.log()语句:
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 60000);
console.log("reset");
// 1000 milisec = 1 sec
}
现在你可以这样使用它了
var timer = new InactivityTimer("/logout", 60000);
// maybe some other event stops the timer?
timer.stop();
// something else starts it back up
timer.start();
您是否已尝试添加console.log()消息?
var timer = new InactivityTimer("/logout", 60000);
// maybe some other event stops the timer?
timer.stop();
// something else starts it back up
timer.start();