vue 指定页面开启定时,超时则进入首页

vue yekong 558℃

vue 进入指定页面定时监听,超过10分钟不操作,进入首页

export default {
  data() {
    return {
      timer: null,
      timeOut: 10 * 60 * 1000,
      enterpriseId: this.$route.query.enterpriseId,
    }
  },
  components: {},
  created() {
  },
  mounted() {
    this.createTiming()
  },
  destroyed() {
    this.destroyTiming()
  },
  watch: {
    $route(to, from) {
      if (this.$route.path != '/videoList') {
        this.destroyTiming()
      } else {
        this.createTiming()
      }
    }
  },
  methods: {
    destroyTiming() {
      clearInterval(this.timer);
      console.log('销毁定时')
    },
    createTiming() {
      localStorage.setItem("lastTime", new Date().getTime());
      // 每3秒 调用检查时间的方法
      // 创建定时
      this.timer = setInterval(this.checkTimeout, 3000);
      // 页面监听 按下鼠标更新操作时间
      window.onload = function () {
        window.document.onmousedown = function () {
          localStorage.setItem("lastTime", new Date().getTime());
        }
      };
    },
    checkTimeout() {
      var that = this;
      //更新当前时间
      let currentTime = new Date().getTime();
      let lastTime = localStorage.getItem("lastTime");
      //判断是否超时
      console.log(lastTime)
      if (currentTime - lastTime > this.timeOut) {
        // 调用自己的注销接口
        localStorage.removeItem("lastTime")
        that.$router.push({path: '/', query: {enterpriseId: this.$route.query.enterpriseId}});
      }
    },
    goback() {
      this.$router.go(-1)
    },
  },
}
喜欢 (0)