需求说明,后台有做半个小时不请求接口的话返回标识退出登录,但是要请求接口才行,现在要实现前端用js判断半个小时不操作界面的话自动跳转到登录页面。创建一个.js文件,在main.js引入此js(vue框架)在登录成功的时候保存当前时间localStorage.setItem("lastTime",new Date().getTime());localStorage.setItem("lastTime",new Date().getTime());然后在点击的时候更新这个时间
var lastTime = new Date().getTime();
var currentTime = new Date().getTime();
var timeOut = 30 * 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*)#/)[1];



window.open(newUrl + '#/login','_self');

}
}

/* 定时器 间隔30秒检测是否长时间未操作页面 */
window.setInterval(checkTimeout, 30000);
var lastTime = new Date().getTime();
var currentTime = new Date().getTime();
var timeOut = 30 * 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*)#/)[1];



window.open(newUrl + '#/login','_self');

}
}

/* 定时器 间隔30秒检测是否长时间未操作页面 */
window.setInterval(checkTimeout, 30000);每隔30s去检查一下是否过了30分钟。总结总结总结以上所述是小编给大家介绍的vue中js判断长时间不操作界面自动退出登录,希望对大家有所帮助!