当前位置:网站首页>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;
边栏推荐
猜你喜欢
数据一致性:双删为什么要延时?
Conditional Statements for Shell Programming
pikachu Over permission
亿流量大考(2):开发一套高容错分布式系统
用于流动质押和收益生成的 Web3 基础设施
113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
投资性大于游戏性 NFT游戏到底是不是门好生意
趣链的产品构架
VLAN实验
随机推荐
HCIP第十五天
The development status of cloud computing at home and abroad
一个函数有多少种调用方式?
483. Smallest Good Base
L2-029 特立独行的幸福
480. Sliding Window Median
win10系统下yolov5-V6.1版本的tensorrt部署细节教程及bug修改
2022-08-03 Oracle executes slow SQL-Q17 comparison
for循环练习题
【day6】类与对象、封装、构造方法
CAS:1620523-64-9_Azide-SS-biotin_biotin-disulfide-azide
举一个 web worker 的例子
Work Subtotal QT Packing
UVa 10003 - Cutting Sticks (White Book, Interval DP)
node连接mysql数据库报错:Client does not support authentication protocol requested by server
21天打卡挑战学习MySQL——《MySQL工具的使用》第一周 第二篇
How to write a database document management tool based on WPF (2)
Cisco ike2 IPSec configuration
亿流量大考(2):开发一套高容错分布式系统
21天打卡挑战学习MySQL—Day第一周 第一篇