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

Web Worker 简单使用

通信方法:

(1) 发送消息
主线程:worker.postMessage()
子线程:self.postMessage()
(2) 接收消息
主线程:worker.onmessage()
子线程:self.onmessage()
(3) 监听异常
主线程:worker.onerror()
子线程:self.onerror()
(4) 销毁方法
主线程:worker.terminate()
子线程:self.close()
(5) 加载脚本 (worker内部加载其他脚本)
importScripts('script1', 'script2')

// 主进程
<template>
  <div class="menu-contain card">
    <el-button type="primary" @click="handlerSendMsg">Send</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const worker = ref()

worker.value = new Worker(new URL('@/workers/handlerArrWorker.ts', import.meta.url))

worker.value.onmessage = (res: any) => {
  console.log(res, 'res')
}

const handlerSendMsg = () => {
  worker.value.postMessage({msg: 'start'})

  console.log(worker.value, 'worker.value')
}
</script>

<style scoped lang="scss">

</style>

 

// 子进程
self.onmessage = () => {
    let list = []
    for (let index = 0; index < 1000000; index++) {
        list.push(Date.now())
    }

    self.postMessage(list)
}


http://www.kler.cn/news/312073.html

相关文章:

  • 2024永久激活版 Studio One 6 Pro for mac 音乐创作编辑软件 完美兼容
  • 基于STM32设计的路灯故障定位系统(微信小程序)(229)
  • flink自定义process,使用状态求历史总和(scala)
  • spring boot启动报错:so that it conforms to the canonical names requirements
  • 【系统架构设计师-2017年真题】案例分析-答案及详解
  • C# Socket网络通信【高并发场景】
  • 【QT】重载信号Connect链接使用方式
  • cuda中使用二维矩阵
  • SpringCloud系列之一---搭建高可用的Eureka注册中心
  • 使用密钥文件登陆Linux服务器
  • 【论文阅读】Grounding Language with Visual Affordances over Unstructured Data
  • 【vue3】vue3.3新特性真香
  • 兔子检测系统源码分享
  • AtCoder Beginner Contest 371
  • 在SpringCloud中实现服务熔断与降级,保障系统稳定性
  • vue2中字符串动态拼接字段给到接口
  • 机器学习(西瓜书)第 14 章 概率图模型
  • 程序员的养生
  • NET WPF使用组件库HandyControl
  • Github 2024-09-14 Rust开源项目日报Top10
  • 傅里叶变换
  • vim 操作一列数字
  • 【天怡AI-注册安全分析报告-无验证方式导致安全隐患】
  • javascript 浏览器打印不同页面设置方向,横向纵向打印
  • CPLEX+Yalmip+MATLAB2022a配置
  • 【贪心算法】贪心算法一
  • vue前端调起电脑应用并且检测有没有调用成功
  • 人工智能将来好就业吗?
  • LINUX的PHY抽象层——PAL
  • Qt/C++ 了解NTFS文件系统,解析盘符引导扇区数据获取MFT(Master File Table)主文件表偏移地址