当前位置:网站首页>Take an example of a web worker
Take an example of a web worker
2022-08-03 22:27:00 【Ziwei front end】
You need to follow the steps below to get started with web workersCount example
- Create a Web Worker file: You need to write a script to increment the count value.Let's name it counter.js
let i = 0; function timedCount() { i = i + 1; postMessage(i);setTimeout("timedCount()", 500); } timedCount();
Here the postMessage() method is used to post the message backto HTML page
- Create Web Worker Object: You can create a Web Worker object by checking browser support.Let's name this file web_worker_example.js
if (typeof w == "undefined") { w = new Worker("counter.js"); }
We can receive messages from web workers
w.onmessage = function (event) { document.getElementById("message").innerHTML = event.data; };
- Terminate Web Worker: The Web Worker will continue to listen for messages (even after the external script completes) until it is terminated.You can use the terminate() method to terminate listening for messages.
w.terminate();
- Reuse Web Workers: If the worker variable is set to undefined, code can be reused
w = undefined;
边栏推荐
- golang写的存储引擎,基于b+树,mmap
- 七夕快乐!
- 嵌入式开发:嵌入式基础——代码和数据空间揭秘
- Testng监听器
- 2022-08-03 Oracle executes slow SQL-Q17 comparison
- Data_web(九)mongodb增量同步到mongodb
- Causes of Mysql Disk Holes and Several Ways to Rebuild Tables
- 如何设计 DAO 的 PoW 评判标准 并平衡不可能三角
- 图的基础概念
- Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
猜你喜欢
随机推荐
CAS:153162-70-0_N-BOC-6-Biotinamidohexylamine
Boss: There are too many systems in the company, can you realize account interoperability?
【MySQL进阶】数据库与表的创建和管理
First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
线上服务器老是卡,该如何优化?
2022-08-02 mysql/stonedb慢SQL-Q18-内存使用暴涨分析
嵌入式系统:时钟
21天打卡挑战学习MySQL——《Window下安装MySql》第一周 第三篇
386. Lexicographical Numbers
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
九种方式,教你读取 resources 目录下的文件路径
老板:公司系统太多,能不能实现账号互通?
什么是memoization,它有什么用?
384. Shuffle an Array
藏宝计划TreasureProject(TPC)系统模式开发技术原理
Codeup brushing notes - simple simulation
用于流动质押和收益生成的 Web3 基础设施
HCIP BGP实验报告
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Kubernetes入门到精通-Operator 模式