Web Worker 是一个很有意思的东东
简单理解一下
0 new Worker("src"); 创建一个worker
1 onmessage 接受信息
2 postMessage 发送信息
代码:
<!DOCTYPE html > <html> <head> <title></title> </head> <body> <div id="show"></div> </body> <script> //web worker var show = document.getElementById("show"); if (typeof (Worker) !== "undefined") { var worker = new Worker("demo.js"); worker.postMessage("hello world!"); worker.onmessage = function (event) { var data = event.data; show.innerHTML = data; } } else { show.innerHTML = "your browser does not support web worker"; } </script> </html>
demo.js 部分:
//postMessage("hello world!"); onmessage = function (event) { var data = event.data; postMessage(data); };