当前位置:网站首页>How to determine if a web worker has been created or closed
How to determine if a web worker has been created or closed
2022-06-25 13:21:00 【acgCode】
Web Worker It can be for JavaScript Create a multithreaded environment , Some tasks can be assigned to sub threading , So as not to block the main thread . adopt Web Worker The created thread splitting process and the main thread run at the same time without interfering with each other . Concrete Web Worker Knowledge about , You can refer to Ruan Yifeng's blog .
Web Worker In the process of use, it should be noted that after the thread is created , In order to respond to the main thread at any time, the communication will always run in the background . therefore Worker It consumes more resources , And will not automatically close .
Of course, in order to solve this problem Web Worker It provides us with the method of manual shutdown , So how do you know Worker What about the current status ? We need to borrow Chrome Developer tools for confirmation .
First of all, the previous code :
<!-- * @Description: html5 Of Web Workers * @FilePath: /JavaScript Basics /WebWorker/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Web Worker</title>
</head>
<body>
<input type="text" name="number" id="number" />
<button id="btn"> Calculation </button>
<script> var input = document.querySelector("#number"); document.querySelector("#btn").onclick = function () {
var number = input.value; var anotherWorker = new Worker("anotherWorker.js"); anotherWorker.onmessage = function (event) {
console.log(event.data); }; anotherWorker.postMessage(" towards anotherWorker Send a message "); }; </script>
</body>
</html>
/* * @FilePath: /JavaScript Basics /WebWorker/anotherWorker.js */
(function () {
this.onmessage = function (event) {
console.log(event.data);
postMessage("anotherWorker Return the data ");
};
})();
The implementation effect is as follows :
here , stay Chrome Of developer tools Sources Label under Page The tree is like this :
This red box is what we created Worker.
After several calls, we found that :
new Worker Constantly creating , old Worker It doesn't shut down automatically . This will cause a waste of resources . that , We need to close it manually Worker.
Let's give it first Worker Add one more close().
/* * @FilePath: /JavaScript Basics /WebWorker/anotherWorker.js */
(function () {
this.onmessage = function (event) {
console.log(event.data);
postMessage("anotherWorker Return the data ");
this.close(); // Close this after the data is returned from the branch line Worker
};
})();
The operation effect is as follows :
Worker The normal operation .
from Sources Label under Page You can see in the tree , these Worker It is completely closed after the data is returned .
Of course , You can also close in the callback of the main thread Worker, The effect is the same . The timing is different , On demand access .
var anotherWorker = new Worker("anotherWorker.js");
anotherWorker.onmessage = function (event) {
console.log(event.data);
anotherWorker.terminate(); // Close the main thread Worker
};
anotherWorker.postMessage(" towards anotherWorker Send a message ");
To sum up :
- Worker Can't close automatically , We need to manually close
- Worker It can be passed through threads
self.close()Shut down , You can also pass through the main threadworker.terminate()Shut down- We can go through Chrome And developer tools Sources Label under Page Tree to observe Worker The state of , To determine Worker Whether it has been successfully created / close .
边栏推荐
- It's an artifact to launch a website in a few minutes
- 剑指Offer 第 2 天链表(简单)
- New Gospel of drug design: Tencent, together with China University of science and technology and Zhejiang University, developed an adaptive graph learning method to predict molecular interactions and
- Regular match the phone number and replace the fourth to seventh digits of the phone number with****
- Custom vertical table
- Golang keyboard input statement scanln scanf code example
- 关于数据在内存中的存储下
- Sword finger offer 04 Find in 2D array
- Sword finger offer day 3 string (simple)
- 关于结构体,枚举,联合的一些知识
猜你喜欢

Koa frame

Golang keyboard input statement scanln scanf code example

药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质

The starting point for learning programming.

Sword finger offer II 025 Adding two numbers in a linked list

Some knowledge of the initial C language

爱可可AI前沿推介(6.25)

关于一道教材题的讲解

初始c语言的知识2.0

[pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
随机推荐
一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推
[pit avoidance means "difficult"] actionref current. Reload() does not take effect
leetcode:456. 132 模式【单调栈】
Sword finger offer day 1 stack and queue (simple)
[machine learning] model and cost function
Introduction to string 18 lectures Collection 4
关于数据在内存中存储的相关例题
Assemble relevant knowledge points of flag bit (connected)
WIN10环境下配置pytorch
Solve the problem that yarn cannot load files in vs Code
Used in time filter (EL table)
[machine learning] parameter learning and gradient descent
OpenStack学习笔记之-Nova组件深入了解
Optimization of lazyagg query rewriting in parsing data warehouse
[pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
MySQL learning notes
leetcode:剑指 Offer II 091. 粉刷房子【二维dp】
Openstack -- creating virtual machines for Nova source code analysis
Implementation of a small book system
Maui的学习之路(二)--设置