let timeOut = 30 * 60 * 1000 //设置超时时间: 30分钟
// 鼠标移动时,将当前时间存储
window.document.onmousedown = function () {
localStorage.setItem("lastTime", new Date().getTime());
}
// 判断用户的未操作的时间
function checkTimeout() {
currentTime = new Date().getTime();
lastTime = localStorage.getItem("lastTime");
if (currentTime - lastTime > timeOut) {
if (localStorage.getItem("token")) {
//请求后台退出登录
}
}
}
// 设置三十分钟检查一次用户的未操作时长
setInterval(checkTimeout, 1000);
前端实现用户一定时间内未操作自动退出
于 2022-11-04 01:11:38 首次发布