vue中超时登出

418 阅读1分钟

超时登出

//超时自动登出
var lastTime = new Date().getTime();
var currentTime = new Date().getTime();
var timeOut = 20 * 60 * 1000; //设置超时时间: 20分

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*)#/)[1];
        console.log("超时登出啦!!!!")
        window.open(newUrl + '#/login','_self');// 这段换成你自己要登出的页面路由
    }
}

/* 定时器 间隔30秒检测是否长时间未操作页面 */
window.setInterval(checkTimeout, 30000);


然后在main.js中引入