gpt4 book ai didi

javascript - 使用系统时钟进行准确的空闲超时警告

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:41:29 24 4
gpt4 key购买 nike

我正在开发一种模式,它会提醒用户由于空闲时间而挂起的注销。总的来说它有效,但我注意到计时器不是 100% 准确。我知道 setInterval 和准确性存在问题。我仍然看到差异,欢迎就如何改进我的代码提出建议。

var sessionTimeoutPeriod = 240;
var state = "L";
var timeoutPeriod = sessionTimeoutPeriod * 1000;
var logout = Math.floor(timeoutPeriod / 60000);
var idleInterval;

if (state == "L" || state == "O" || state == "C") {
var idleTime = 0;

$(document).ready(function() {
//Zero the idle timer on mouse movement.
$(this).mousemove(function(e) {
idleTime = 0;
console.log("The mouse moved, idle time = " + idleTime);
});
$(this).keypress(function(e) {
idleTime = 0;
console.log("A key was pressed, idle time = " + idleTime);
});


idleInterval = setInterval(timerIncrement, 60000); // 1 minute

});

function timerIncrement() {

idleTime++;
console.log("The total idle time is "+idleTime+ " minutes.");

if (idleTime >= 1) {
console.log("The modal will fire if idle time = " +idleTime);
var modal = new window.AccessibleModal({
mainPage: $('#main'),
overlay: $('#overlay').css('display', 'block'),
modal: $('#modal-session-timeout'),
prevFocus: $('#main')
});

modal.show();

$('#modal-overlay').removeClass('opened');

$('.js-timeout-refresh, .xClose').click(function() {
modal.hide();
$('#overlayTY').css('display', 'none');
idleTime = 0;

console.log("The total idle time is "+idleTime+ " minutes.");
});
$('.js-timeout-session-end').click(function() {
modal.hide();
$('#overlay').css('display', 'none');
endSession();
});
console.log(idleTime);
}
if (idleTime == logout) { // 9 minutes
endSession();
}

var endSession = function() {
document.location.replace(logoutPageUrl);
};
}

最佳答案

您可以通过比较当前时间与自上次事件以来的时间来手动验证已经过去了多少时间,而不是依赖间隔来确定已经过去了多少时间。

这样您就可以增加检查间隔而无需修改代码。差异永远不会高于现在的间隔超时,差异也不会递增变化,它将始终低于或等于间隔(在最坏的情况下)。

这是一个基本的例子。

var sessionTimeoutPeriod = 240;
var timeoutPeriod = sessionTimeoutPeriod * 1000;
var oneMinute = 60000;
var showIdlePopUpTimeOut = oneMinute * 2;
var lastActivity = new Date();

function getIdleTime() {
return new Date().getTime() - lastActivity.getTime();
}

$(document).ready(function () {
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
console.log("The mouse moved, idle time = " + getIdleTime());
lastActivity = new Date();
});
$(this).keypress(function (e) {
console.log("A key was pressed, idle time = " + getIdleTime());
lastActivity = new Date();
});

setInterval(checkIdleTime, 5000);
});

function endSession() {
document.location.replace(logoutPageUrl);
};

function checkIdleTime() {
var idleTime = getIdleTime();
console.log("The total idle time is " + idleTime / oneMinute + " minutes.");

if (idleTime > showIdlePopUpTimeOut) {
var modal = new window.AccessibleModal({
mainPage: $('#main'),
overlay: $('#overlay').css('display', 'block'),
modal: $('#modal-session-timeout'),
prevFocus: $('#main')
});

modal.show();

$('#modal-overlay').removeClass('opened');

$('.js-timeout-refresh, .xClose').click(function () {
modal.hide();
$('#overlayTY').css('display', 'none');
lastActivity = new Date();
});
$('.js-timeout-session-end').click(function () {
modal.hide();
$('#overlay').css('display', 'none');
endSession();
});
console.log(lastActivity);
}
if (idleTime > timeoutPeriod) { // 4 minutes
endSession();
}

}

关于javascript - 使用系统时钟进行准确的空闲超时警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37618733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com