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

先贴代码,有时间在解释

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 天前
Vue 基础:状态管理入门
前端·vue.js
JavaGuide1 天前
利用元旦假期,我开源了一个大模型智能面试平台+知识库!
前端·后端
yuanyxh1 天前
程序设计
前端·设计
坚持就完事了1 天前
JavaScript
开发语言·javascript·ecmascript
eason_fan1 天前
前端性能优化利器:LitePage 轻量级全页设计解析
前端·性能优化·前端工程化
pe7er1 天前
如何阅读英文文档
java·前端·后端
先做个垃圾出来………1 天前
Python位运算及操作
java·前端·python
daqinzl1 天前
JavaScript连接WebSocket
javascript·websocket
梦帮科技1 天前
第三十四篇:开源社区运营:GitHub Stars增长策略
开发语言·前端·爬虫·python·docker·架构·html
POLITE31 天前
Leetcode 19. 删除链表的倒数第 N 个结点 JavaScript (Day 11)
javascript·leetcode·链表