当前位置:网站首页>举一个 web worker 的例子
举一个 web worker 的例子
2022-08-03 22:22:00 【紫微前端】
您需要按照以下步骤开始使用网络工作者进行计数示例
- 创建一个 Web Worker 文件:您需要编写一个脚本来增加计数值。我们将其命名为 counter.js
let i = 0; function timedCount() { i = i + 1; postMessage(i); setTimeout("timedCount()", 500); } timedCount();这里 postMessage() 方法用于将消息回传到 HTML 页面
- 创建 Web Worker 对象:您可以通过检查浏览器支持来创建 Web Worker 对象。让我们将此文件命名为 web_worker_example.js
if (typeof w == "undefined") { w = new Worker("counter.js"); }我们可以接收来自 web worker 的消息
w.onmessage = function (event) { document.getElementById("message").innerHTML = event.data; };- 终止 Web Worker:Web Worker 将继续侦听消息(即使在外部脚本完成后),直到它被终止。您可以使用 terminate() 方法来终止侦听消息。
w.terminate();- 重用 Web Worker:如果将 worker 变量设置为 undefined,则可以重用代码
w = undefined;边栏推荐
猜你喜欢

嵌入式系统:时钟

CAS: 1192802-98-4 _uv cracking of biotin - PEG2 - azide

CAS:908007-17-0_Biotin-azide _生物素叠氮化物

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!

Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记

CAS:1797415-74-7_TAMRA-Azide-PEG-Biotin

encapsulation, package, access modifier, static variable

亿流量大考(2):开发一套高容错分布式系统
![[N1CTF 2018]eating_cms](/img/09/3599d889d9007eb45c6eab3043f0c4.png)
[N1CTF 2018]eating_cms

HCIP第十六天
随机推荐
超级实用网站+公众号合集
PowerMockup 4.3.4::::Crack
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo
Boss: There are too many systems in the company, can you realize account interoperability?
嵌入式系统:GPIO
start with connect by 实现递归查询
决策树、GBDT、XGBOOST树的可视化
Golang Chapter 1: Getting Started
Makefile
藏宝计划TreasureProject(TPC)系统模式开发技术原理
Embedded systems: overview
for loop exercises
E-commerce data warehouse ODS layer-----log data loading
CAS:908007-17-0_Biotin-azide_Biotin azide
LitJson报错记录
382. Linked List Random Node
CAS:1192802-98-4_UV 裂解的生物素-PEG2-叠氮
Embedded Systems: GPIO
Unification of east-west and north-south communications