表单长时间无响应,页面失效提示

先贴代码,有时间在解释

html 复制代码
<template>
  <div :class="$style.root">
    <div>主页面</div>
    <el-input v-model="txt" @input="onChange" />
  </div>
</template>
<script>
const TIME = 10; //时间,用于限定多长时间无操作提醒
export default {
  data() {
    return {
      txt: 1,
      worker: null,
    };
  },
  mounted() {
    this.worker = new Worker('/worker.js');

    this.worker.postMessage('start');
    this.worker.addEventListener('message', e => {
      console.log('e: ', e.data);
      const { times } = e.data;
      if (times >= TIME) {
        this.$message.error('页面失效');
        // 结束通信
        this.worker.postMessage('end');
      }
    });
    document.addEventListener('visibilitychange', function () {
      if (document.visibilityState == 'visible') {
        console.log('页面已显示');
      } else if (document.visibilityState == 'hidden') {
        console.log('页面已隐藏');
      }
    });
  },
  methods: {
    onChange() {
      this.worker.postMessage('end');
      this.worker.postMessage('start');
    },
  },
};
</script>
<style lang="scss" module>
@import '../../styles/common';

.root {
  @include center;
}
</style>

// public下面的worker.js

javascript 复制代码
let timer;
self.addEventListener('message', e => {
  let times = 0;
  let msg;
  if (e.data == 'start') {
    timer = setInterval(() => {
      times++;
      msg = {
        status: 'editing',
        times,
      };
      self.postMessage(msg);
    }, 1 * 1000);
  } else {
    times=0;
    clearInterval(timer);
  }
});
相关推荐
熊猫钓鱼>_>1 小时前
建筑IT数字化突围:建筑设计企业的生存法则重塑
前端·javascript·easyui
GISer_Jing3 小时前
前端性能指标及优化策略——从加载、渲染和交互阶段分别解读详解并以Webpack+Vue项目为例进行解读
前端·javascript·vue
不知几秋3 小时前
数字取证-内存取证(volatility)
java·linux·前端
水银嘻嘻4 小时前
08 web 自动化之 PO 设计模式详解
前端·自动化
Zero1017136 小时前
【详解pnpm、npm、yarn区别】
前端·react.js·前端框架
&白帝&6 小时前
vue右键显示菜单
前端·javascript·vue.js
Wannaer6 小时前
从 Vue3 回望 Vue2:事件总线的前世今生
前端·javascript·vue.js
羽球知道7 小时前
在Spark搭建YARN
前端·javascript·ajax
光影少年7 小时前
vue中,created和mounted两个钩子之间调用时差值受什么影响
前端·javascript·vue.js
青苔猿猿7 小时前
node版本.node版本、npm版本和pnpm版本对应
前端·npm·node.js·pnpm