zy-多线程调试worker

This commit is contained in:
luzhisheng 2022-03-04 18:11:48 +08:00
parent cf11277437
commit 850692c6d2
5 changed files with 58 additions and 1 deletions

View File

@ -242,3 +242,7 @@ Babel 手册
ayf
{ code: 'var a = "ayf";', map: null, rawMappings: undefined }
var a = "ayf";
babel-types
https://www.babeljs.cn/docs/babel-types

View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="main.js"></script>
<title>测试</title>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,3 @@
console.log(111);
var worker = new Worker('work.js');
worker.postMessage('ayf');

View File

@ -0,0 +1,4 @@
console.log(self);
self.addEventListener('message', function (e) {
console.log('You said: ' + e.data);
}, false);

View File

@ -0,0 +1,35 @@
## 多线程
如何开启线程
console.log(111);
var worker = new Worker('work.js');
线程与线程传值问题
worker.postMessage('ayf');
接收
console.log(self);
self.addEventListener('message', function (e) {
console.log('You said: ' + e.data);
}, false);
拿到返回的子线程返回数据
self.onmessage = function (e) {
var uInt8Array = e.data;
postMessage('Inside worker.js: uInt8Array.toString() = ' + uInt8Array.toString());
postMessage('Inside worker.js: uInt8Array.byteLength = ' + uInt8Array.byteLength);
};
得到几个关键词
主线程 Worker onmessage postMessage
子线程 self onmessage postMessage
经过案例,我发现能通过搜索的上面的关键词快速的定位
主线程不能直接调用子线程的方法