当前位置: 首页 > news >正文

vue2中使用web worker启动定时器

vue2中使用web worker启动定时器,避免浏览器最小化或切换标签页时定时器不能按设定周期执行【一般是周期小于60s时,大于60s一般可正常执行】

  • 1、添加worker-loader
  • 2、修改vue.config.js
  • 3、创建timer.worker.js
  • 4、创建TimerWorker.js
  • 5、使用TimerWorker启动定时任务

1、添加worker-loader

npm install worker-loader --save

2、修改vue.config.js

新增以下配置

 config.module.rule('workers').test(/\.worker\.js$/).use('worker-loader').loader('worker-loader').options({inline: 'fallback'  // 尝试内联,失败则回退到默认行为})

3、创建timer.worker.js

self.timers = {}self.onmessage = function(event) {const { action, id, interval } = event.dataswitch (action) {case 'startTimer':startTimer(id, interval)breakcase 'stopTimer':stopTimer(id)breakcase 'stopAllTimers':stopAllTimers()break}
}function startTimer(id, interval) {if (!self.timers[id]) {self.timers[id] = setInterval(() => {self.postMessage({ id })}, interval)}
}function stopTimer(id) {if (self.timers[id]) {clearInterval(self.timers[id])delete self.timers[id]}
}function stopAllTimers() {Object.keys(self.timers).forEach(id => {clearInterval(self.timers[id])})self.timers = {}
}

4、创建TimerWorker.js

import Worker from './timer.worker.js'export default class TimerWorker {worker;timers;constructor() {this.start()}startTimer(id, interval, fun) {if (this.worker && !this.timers[id]) {this.timers[id] = funthis.worker.postMessage({ action: 'startTimer', id, interval })}return this}stopTimer(id) {if (this.worker && this.timers[id]) {this.worker.postMessage({ action: 'stopTimer', id })delete this.timers[id]}return this}stopAllTimers() {if (this.worker) {this.worker.postMessage({ action: 'stopAllTimers' })this.timers = {}}return this}start() {if (!this.worker) {const worker = new Worker()worker.onmessage = (event) => {this.timers[event.data.id]()}this.worker = workerthis.timers = {}}return this}terminate() {if (this.worker) {this.stopAllTimers()this.worker.terminate()this.worker = undefinedthis.timers = undefined}return this}
}

5、使用TimerWorker启动定时任务

import TimerWorker from './TimerWorker'export default {...created() {this._timerWorker = new TimeWorker()this.startTimer()},beforeDestroy() {this._timerWorker.terminate()},methods: {startTimer() {this._timeWorker.startTimer('test', 100, () => {console.log(new Date().getTime())})}}...
}
http://www.lryc.cn/news/435794.html

相关文章:

  • 【Python 学习】Numpy的基础和应用
  • 基于python+django+vue+MySQL的酒店推荐系统
  • 什么是 PD 电压诱骗?
  • 【漏洞复现】用友 NC pagesServlet Sql注入漏洞
  • 边缘检测运用
  • 应用宝自动下载安装
  • Vue 2 中实现双击事件的几种方法
  • windows服务管理插件 nssm
  • 【读书笔记-《30天自制操作系统》-19】Day20
  • Kubernetes服务注册与发现
  • 【 html+css 绚丽Loading 】000047 玄武流转盘
  • 线程池原理及改造
  • 彻底理解mysql Buffer Pool (拓展)
  • 信号量(二值信号量和计数信号量)和互斥量
  • 结构型模式-python版
  • Java重修笔记 第五十四天 坦克大战(二)常用的绘图方法、画出坦克图形
  • OpenAI澄清:“GPT Next”不是新模型。
  • <<编码>> 第 10 章 逻辑与开关(Logic and Switches) 示例电路
  • 深入浅出 Ansible 自动化运维:从入门到实战
  • 一句话描述设计模式
  • 【Linux】Ubuntu 22.04 shell实现MySQL5.7 tar 一键安装
  • SQL Server开启网络访问
  • el-input设置type=‘number‘和v-model.number的区别
  • 6.第二阶段x86游戏实战2-理解程序流程
  • Netty笔记01-Netty的基本概念与用法
  • OpenHarmony鸿蒙( Beta5.0)RTSPServer实现播放视频详解
  • QT使用事件事件和绘制事件实现简易时钟
  • kubeadm方式安装k8s
  • 如何使用go生成可执行文件
  • 手写Promise