我试图在空闲时解雇用户,跨标签使用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>