Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[html] 第53天 web workers有用过吗?能帮我们解决哪些问题? #207

Open
haizhilin2013 opened this issue Jun 7, 2019 · 6 comments
Labels
html html

Comments

@haizhilin2013
Copy link
Collaborator

第53天 web workers有用过吗?能帮我们解决哪些问题?

@haizhilin2013 haizhilin2013 added the html html label Jun 7, 2019
@DarthVaderrr
Copy link

DarthVaderrr commented Jul 4, 2019

提供协程能力,如果有一个比较密集的计算任务,可以放到另一个进程中处理,等处理好了再把结果传回主程,这样主要进程就不会阻塞,页面可以正常渲染和响应

@justfn
Copy link

justfn commented Sep 11, 2019

提供协程能力,如果有一个比较密集的计算任务,可以放到另一个进程中处理,等处理好了再把结果传回主程,这样主要进程就不会阻塞,页面可以正常渲染和响应

补充一点: 可充分利用多核的CPU

@censek
Copy link

censek commented Nov 22, 2019

为 JavaScript 创造多线程环境,允许主线程创建 Worker 线程,将一些任务分配给后者运行。
http://www.ruanyifeng.com/blog/2018/07/web-worker.html

@MrZ2019
Copy link

MrZ2019 commented Nov 20, 2020

提供协程能力,如果有一个比较密集的计算任务,可以放到另一个进程中处理,等处理好了再把结果传回主程,这样主要进程就不会阻塞,页面可以正常渲染和响应

@d4c-27
Copy link

d4c-27 commented Apr 5, 2022

Js是单线程执行的,H5提出实现多线程:Web Workers(内部代码不能操作DOM因为this不是window)、不能跨域加载js、不是所有浏览器都能支持
Worker.prototype.onmessage:用于接收另一个线程的回调函数
Worker.prototype.postMessage:向另一个线程发送消息

@xiaoxiaozhiya
Copy link

  • Web Socket提供多线程
  • 缺点:慢
  • 不能跨域访问Dom(涉及死锁和同步)
  • 域名是虚拟路径
  • 不是每个浏览器都支持
    `
    //首先创建一个对象
    var worker=new Worker('worker.js')

//绑定接收消息的监听
worker.onmessage=function(event){
console.log(event.data)
}

//发送消息
worker.postmessage(number)

`

在分线程的文件中
// 接收消息:用表达式声明一个函数 var msg=function(event){ var number =event.data //发送消息 postMessage(result) }

总结:在worker中,接收消息的监听 ,worker.onmessage

发送数据:worker.postmessage

在分线程的文件:接收数据用表达式声明一个函数

发送数据:postMessage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
html html
Projects
None yet
Development

No branches or pull requests

7 participants