Javascript 设置睡眠计时器超时失败,单击“上一步”按钮

Javascript 设置睡眠计时器超时失败,单击“上一步”按钮,javascript,jquery,timer,settimeout,back,Javascript,Jquery,Timer,Settimeout,Back,我有一个睡眠计时器,如果用户的计算机睡眠时间超过30秒,它将注销用户 如果用户在站点上单击后退按钮一次或多次,他们就会注销,但实际上并不一致 我知道setTimeout()会暂停,然后在用户单击后退按钮时恢复,那么为什么它会注销用户 (function ($) { /** * @property {number} warningTimeout Time (ms) of the countdown until logout. * @property {number} slee

我有一个睡眠计时器,如果用户的计算机睡眠时间超过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);

你有没有这方面的“有效”例子?我猜您没有使用cookies或存储会话变量。我还没有听说过使用jQuery来如此有效地管理时间。看看@TimWilson我没有一个有效的例子,我们的网站不是对所有人开放的。我还没有使用cookies或会话,我只是想让计时器暂时工作,然后稍后再添加。我希望计时器在用户打开页面时工作。在页面加载之间注销用户将需要不同的脚本。