gpt4 book ai didi

javascript - 如何知道浏览器空闲时间?

转载 作者:行者123 更新时间:2023-12-03 12:22:18 24 4
gpt4 key购买 nike

如何跟踪浏览器空闲时间?我正在使用 IE8。

我没有使用任何 session 管理,也不想在服务器端处理它。

最佳答案

这是跟踪空闲时间的纯 JavaScript 方法,并在达到一定限制时执行一些操作:

var IDLE_TIMEOUT = 60; //seconds
var _idleSecondsTimer = null;
var _idleSecondsCounter = 0;

document.onclick = function() {
_idleSecondsCounter = 0;
};

document.onmousemove = function() {
_idleSecondsCounter = 0;
};

document.onkeypress = function() {
_idleSecondsCounter = 0;
};

_idleSecondsTimer = window.setInterval(CheckIdleTime, 1000);

function CheckIdleTime() {
_idleSecondsCounter++;
var oPanel = document.getElementById("SecondsUntilExpire");
if (oPanel)
oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + "";
if (_idleSecondsCounter >= IDLE_TIMEOUT) {
window.clearInterval(_idleSecondsTimer);
alert("Time expired!");
document.location.href = "logout.html";
}
}
#SecondsUntilExpire { background-color: yellow; }
You will be auto logged out in <span id="SecondsUntilExpire"></span> seconds.


此代码将在显示警报和重定向之前等待 60 秒,并且任何操作都将“重置”计数 - 鼠标单击、鼠标移动或按键。

它应该尽可能跨浏览器,并且直截了当。如果您向页面添加 ID 为 SecondsUntilExpire 的元素,它还支持显示剩余时间.

上面的代码应该可以正常工作,但是有几个缺点,例如它不允许任何其他事件运行并且不支持多个选项卡。包含这两者的重构代码如下:(无需更改 HTML)
var IDLE_TIMEOUT = 60; //seconds
var _localStorageKey = 'global_countdown_last_reset_timestamp';
var _idleSecondsTimer = null;
var _lastResetTimeStamp = (new Date()).getTime();
var _localStorage = null;

AttachEvent(document, 'click', ResetTime);
AttachEvent(document, 'mousemove', ResetTime);
AttachEvent(document, 'keypress', ResetTime);
AttachEvent(window, 'load', ResetTime);

try {
_localStorage = window.localStorage;
}
catch (ex) {
}

_idleSecondsTimer = window.setInterval(CheckIdleTime, 1000);

function GetLastResetTimeStamp() {
var lastResetTimeStamp = 0;
if (_localStorage) {
lastResetTimeStamp = parseInt(_localStorage[_localStorageKey], 10);
if (isNaN(lastResetTimeStamp) || lastResetTimeStamp < 0)
lastResetTimeStamp = (new Date()).getTime();
} else {
lastResetTimeStamp = _lastResetTimeStamp;
}

return lastResetTimeStamp;
}

function SetLastResetTimeStamp(timeStamp) {
if (_localStorage) {
_localStorage[_localStorageKey] = timeStamp;
} else {
_lastResetTimeStamp = timeStamp;
}
}

function ResetTime() {
SetLastResetTimeStamp((new Date()).getTime());
}

function AttachEvent(element, eventName, eventHandler) {
if (element.addEventListener) {
element.addEventListener(eventName, eventHandler, false);
return true;
} else if (element.attachEvent) {
element.attachEvent('on' + eventName, eventHandler);
return true;
} else {
//nothing to do, browser too old or non standard anyway
return false;
}
}

function WriteProgress(msg) {
var oPanel = document.getElementById("SecondsUntilExpire");
if (oPanel)
oPanel.innerHTML = msg;
else if (console)
console.log(msg);
}

function CheckIdleTime() {
var currentTimeStamp = (new Date()).getTime();
var lastResetTimeStamp = GetLastResetTimeStamp();
var secondsDiff = Math.floor((currentTimeStamp - lastResetTimeStamp) / 1000);
if (secondsDiff <= 0) {
ResetTime();
secondsDiff = 0;
}
WriteProgress((IDLE_TIMEOUT - secondsDiff) + "");
if (secondsDiff >= IDLE_TIMEOUT) {
window.clearInterval(_idleSecondsTimer);
ResetTime();
alert("Time expired!");
document.location.href = "logout.html";
}
}

上面重构的代码使用本地存储来跟踪上次重置计数器的时间,并在每个打开的包含代码的新选项卡上重置它,然后所有选项卡的计数器将相同,并重置一次将导致所有选项卡的重置。由于 Stack Snippets 不允许本地存储,因此将其托管在那里毫无意义,所以我做了一个 fiddle :
http://jsfiddle.net/yahavbr/gpvqa0fj/3/

关于javascript - 如何知道浏览器空闲时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9564602/

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