首页 > 代码库 > Inline Workers--Web workers without a separate Javascript file
Inline Workers--Web workers without a separate Javascript file
<!DOCTYPE html><html><head> <meta charset="utf-8" /></head><body> <div id="log"></div> <script id="worker1" type="javascript/worker"> // This script won‘t be parsed by JS engines because its type is javascript/worker. self.onmessage = function(e) { self.postMessage(‘msg from worker‘); }; // Rest of your worker code goes here. </script> <script> function log(msg) { // Use a fragment: browser will only render/reflow once. var fragment = document.createDocumentFragment(); fragment.appendChild(document.createTextNode(msg)); fragment.appendChild(document.createElement(‘br‘)); document.querySelector("#log").appendChild(fragment); } var blob = new Blob([ document.querySelector(‘#worker1‘).textContent ], { type: "text/javascript" }) // Note: window.webkitURL.createObjectURL() in Chrome 10+. var worker = new Worker(window.URL.createObjectURL(blob)); worker.onmessage = function(e) { log("Received: " + e.data); } worker.postMessage("hello"); // Start the worker. </script></body></html>
https://www.html5rocks.com/en/tutorials/workers/basics/#toc-inlineworkers
Inline Workers--Web workers without a separate Javascript file
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。