当前位置:网站首页>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 .
边栏推荐
- 坡道带来的困惑
- MySQL learning notes
- OpenStack学习笔记(一)
- [data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
- 关于数据在内存中的存储下
- 數據在內存中的存儲相關內容
- 1251- client does not support authentication protocol MySQL error resolution
- Used in time filter (EL table)
- Sword finger offer day 1 stack and queue (simple)
- Component: is to switch between multiple components
猜你喜欢

Shenzhen mintai'an intelligent second side_ The first offer of autumn recruitment

Custom vertical table

Analyse de l'optimisation de la réécriture des requêtes lazyagg de l'entrepôt

Of binary tree_ Huffman tree_ Huffman encoding

Koa frame

Serenvlt first met

中国虚拟人哪家强?沙利文、IDC:小冰百度商汤位列第一梯队

剑指 Offer 第 1 天栈与队列(简单)

揭秘GaussDB(for Redis):全面对比Codis

Drago Education - typescript learning
随机推荐
Openstack learning notes -grace component insight
OpenStack学习笔记(二)
关于三子棋游戏的简易实现与N子棋胜利判断方法
Always maintain epidemic prevention and control and create a safe and stable social environment
Drago Education - typescript learning
Back test of quantitative trading - tqzfuturerenkowavestrategy
OpenStack学习笔记之-Nova组件深入了解
Knowledge of initial C language 2.0
Maui的学习之路(二)--设置
Native JS --- infinite scrolling
Capabilities required by architects
Qt鼠标跟踪
Sword finger offer II 032 Effective anagrams
Summary of leetcode linked list problem solving skills
How unity makes the UI intercept click events
Leetcode: Sword finger offer II 091 Painting house [2D DP]
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon
AGCO AI frontier promotion (6.25)
Implementation of a small book system