【发布时间】:2017-07-10 12:49:40
【问题描述】:
我试图在空闲时解雇用户,跨标签使用 javascript,下面的代码对于单个标签工作正常,对于多个标签它不能正常工作
例如:我已将 10 秒设置为空闲时间,并在 10 秒后将第一个标签页扔出,假设我在 00:00:05 小时打开第一个标签页并打开第二个标签在 00:00:10 小时,并在第二个标签上工作 00:00:13 小时,并离开项目必须在 00 注销的两个标签:00:23 对吧?但是它在 00:00:15 上注销,我不知道这里发生了什么,如果它没有正确刷新,当我使用它时,它怎么会在第二个标签上停留很长时间?如果刷新正常,如何根据第一个打开的选项卡将我注销,代码如下。
<script>
var IDLE_TIMEOUT = 10; //seconds
localStorage.setItem("setTimeOut", "0");
document.onclick = function () {
localStorage.setItem("setTimeOut", "0");
};
document.onmousemove = function () {
localStorage.setItem("setTimeOut", "0");
};
document.onkeypress = function () {
localStorage.setItem("setTimeOut", "0");
};
document.onfocus = function () {
localStorage.setItem("setTimeOut", "0");
};
window.setInterval(CheckIdleTime, 1000);
function CheckIdleTime() {
localStorage.setItem("setTimeOut", parseInt(localStorage.getItem("setTimeOut"))+1);
if (localStorage.getItem("setTimeOut") >= IDLE_TIMEOUT) {
alert('Times up!, You are idle for about 15 minutes, Please login to continue');
document.location.href = "logout.php";
}
}
</script>
【问题讨论】:
-
嘿,你有解决方案吗? ...如果是,请在此处发布功能脚本...因为我也需要同样...谢谢
-
抱歉耽搁了,下面的解决方案对我有用,我被标记为正确。
标签: javascript local-storage logout idle-timer