我有一个睡眠定时器,如果用户的电脑进入睡眠状态超过30秒,它将退出用户。
如果用户点击网站上的后退按钮一次或多次,他们就会被注销,但实际上并不一致。
我知道setTimeout()
暂停,然后在用户点击后退按钮时恢复,那么为什么要注销用户呢?
(function ($) {
/**
* @property {number} warningTimeout Time (ms) of the countdown until logout.
* @property {number} sleepInterval Interval length in time (ms) to check if the computer went to sleep.
* @property {function()} logout Function to redirect user to logout screen.
* @property {?string} sleepTimer Timeout ID of the timer that checks if the computer whent to sleep.
* @property {?number} sleepTime Timestamp of the last time the sleepTimer timeout was executed.
*/
var session = {
warningTimeout: 30000,
sleepInterval: 5000,
logout: function () {
clearTimeout(session.sleepTimer);
clearTimeout(session.keepaliveTimer);
$(document).idleTimer('destroy');
window.location.href = "index.php?q=logout";
},
sleepTimer: null,
sleepTime: Date.now(),
};
/**
* Logs out user if computer goes to sleep
*/
session.sleepTimer = setTimeout(function sleeper() {
var diff = Date.now() - session.sleepTime;
session.sleepTime = Date.now();
// If asleep for 30 seconds or more, logout
if (diff >= session.warningTimeout) {
session.logout();
// Synchronize with the start time every run
} else {
session.sleepTimer = setTimeout(sleeper, session.sleepInterval - (diff % session.sleepInterval));
}
}, session.sleepInterval);
})(jQuery);