flat: 添加30分钟退出功能
This commit is contained in:
30
src/util/backTime.js
Normal file
30
src/util/backTime.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const MAX_IDLE_TIME = 30 * 60 * 1000;
|
||||
let lastActionTime = new Date();
|
||||
import store from "@/store";
|
||||
import router from "@/router/router";
|
||||
|
||||
function updateLastActionTime() {
|
||||
lastActionTime = new Date();
|
||||
}
|
||||
|
||||
// 定义一个计时器
|
||||
let timeoutId = setTimeout(() => {
|
||||
console.log('用户已经很长时间没有操作了!');
|
||||
backLogOut()
|
||||
}, MAX_IDLE_TIME);
|
||||
|
||||
// 监听页面上任何一个元素的点击事件
|
||||
document.addEventListener('click', () => {
|
||||
updateLastActionTime();
|
||||
clearTimeout(timeoutId);
|
||||
timeoutId = setTimeout(() => {
|
||||
console.log('用户已经很长时间没有操作了!');
|
||||
backLogOut()
|
||||
}, MAX_IDLE_TIME);
|
||||
});
|
||||
|
||||
function backLogOut() {
|
||||
store.dispatch("LogOut").then(() => {
|
||||
router.push({path: "/login"});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user