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

先贴代码,有时间在解释

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);
  }
});
相关推荐
Aric_Jones2 小时前
JavaScript 从入门到精通:完整语法指南
开发语言·javascript·ecmascript
岱宗夫up2 小时前
FastAPI入门(上篇):快速构建高性能Python Web API
开发语言·前端·python·fastapi
紫陌涵光3 小时前
112. 路径总和
java·前端·算法
漠月瑾-西安3 小时前
CVE-2025-55182漏洞解析:你的React项目安全吗?
前端·安全·react.js
No丶slovenly3 小时前
flutter笔记-输入框
前端·笔记·flutter
国产化创客4 小时前
ESP32+Web实现智能气象站
前端·物联网·智能家居·智能硬件
coderYYY5 小时前
VSCode终端启动报错
前端·ide·vscode·npm·编辑器
西门吹-禅5 小时前
文本搜索node js--meilisearch
开发语言·javascript·ecmascript
tod1135 小时前
Redis 数据类型与 C++ 客户端实践指南(redis-plus-plus)
前端·c++·redis·bootstrap·html
Sylvia33.5 小时前
火星数据:棒球数据API
java·前端·人工智能