Vue项目设置超时时间后,限定时间内不操作再次操作将退出

首先编写获取设置好的超时时间的方法

 
//查询超时时间设置
selectTime() {
  let that = this;
  if (localStorage.getItem("access-token")) {
    let params = {};
    API.SetTimeFindOne(JSON.stringify(params)).then(res => {
      if (res.returnCode != "200") {
        layer.alert(res.msg, {
          icon: 2, //1 √ 2 ×
          shade: 0.5
        });

        return;
      }
      if (res.returnData.hasOwnProperty('data')) {
     that.timeOut = parseInt(res.returnData.data.minutes) * 60 * 1000;
    }
      
    });
  }else {
        this.$router.push({ name: "login" });
      }
}

created时候获取当前时间作为lasttime,并查询超时时间设置

created() { this.lastTime = new Date().getTime(); this.selectTime();
}

 
鼠标移动事件放在最顶级div
@mousemove="isTimeOut"  
isTimeOut() {
  this.currentTime = new Date().getTime(); // 记录这次点击的时间
  if (this.currentTime - this.lastTime > this.timeOut) {
    // 判断上次最后一次点击的时间和这次点击的时间间隔是否大于30分钟
    if (localStorage.getItem("access-token")) {
      // 如果是登录状态
      localStorage.clear();
      this.$notify({
        title: "提示",
        message: "超时未操作,系统自动退出!"
      });
      if (window.top != null && window.top.document.URL != document.URL) {
        window.top.location.href = `http://${window.location.host}/login`

      } else {
        this.$router.push({ name: "login" });
      }
    } else {
      this.lastTime = new Date().getTime();
      this.$router.push({ name: "login" });
    }
  } else {
    this.lastTime = new Date().getTime(); // 如果在30分钟内点击,则把这次点击的时间记录覆盖掉之前存的最后一次点击的时间
  }
},

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值