基于vue实现单页长时间不操作自动退出登录清除token,返回登录页
首先创建一个astrict.js//单页长时间不操作就会自动退出var lastTime = new Date().getTime()var currentTime = new Date().getTime()var timeOut = 1 * 60 *1000//设置超时时间: 30分window.onload = function() {window.document...
·
首先创建一个astrict.js
//单页长时间不操作就会自动退出
var lastTime = new Date().getTime()
var currentTime = new Date().getTime()
var timeOut = 1 * 60 *1000 //设置超时时间: 30分
window.onload = function() {
window.document.onmousedown = function() {
localStorage.setItem("lastTime",new Date().getTime())
}
};
function checkTimeout() {
currentTime = new Date().getTime() //更新当前时间
lastTime = localStorage.getItem("lastTime");
// console.log(currentTime - lastTime);
// console.log(timeOut);
if (currentTime - lastTime > timeOut) { //判断是否超时
// console.log("超时");
var url = window.location.href;
var newUrl=url.match(/(\S*)#/);
sessionStorage.clear()
window.open('/login','_self');
}
}
/* 定时器 间隔30秒检测是否长时间未操作页面 */
window.setInterval(checkTimeout, 30000);
然后在mian.js引入,注册一下就可以了
import Astrict from './assets/js/astrict.js'
Vue.use(Astrict)
不明白的可以留言共同讨论
更多推荐
已为社区贡献11条内容
所有评论(0)