javascript Worker子线程

main.js code:

 1 //创建
 2 var worker = new Worker("./worker.js");
 3 
 4 //发送
 5 worker.postMessage(1);
 6 
 7 //接收
 8 worker.onmessage = (e)=>{
 9     console.log("main.js:");
10     console.log(e.data);
11 }

worker.js code:

 1 //接收
 2 self.onmessage = (e)=>{
 3 
 4     console.log("worker.js:");
 5     console.log(e.data);
 6     
 7     //返回
 8     self.postMessage(e.data);
 9     
10 }

 

posted @ 2020-02-16 19:04  鸡儿er  阅读(281)  评论(0编辑  收藏  举报