当前位置:网站首页>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;
边栏推荐
猜你喜欢
随机推荐
376. Wiggle Subsequence
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
[b01lers2020]Life on Mars
What is the difference between the generator version and the viewer version?
Embedded systems: overview
Bytebase database schema change management tool
嵌入式系统:概述
为什么我们需要回调
PowerMockup 4.3.4::::Crack
pikachu Over permission
382. Linked List Random Node
Cisco ike2 IPSec配置
utlis 线程池
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Makefile
物联网新零售模式,引领购物新潮流
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
Flink--Join以及Flink函数
线上服务器老是卡,该如何优化?
如何基于WPF写一款数据库文档管理工具(二)