码迷,mamicode.com
首页 > 编程语言 > 详细

Inline Workers--Web workers without a separate Javascript file

时间:2016-12-04 07:47:58      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:html   .text   creat   span   --   asc   class   rom   var   

 

 

<!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

标签:html   .text   creat   span   --   asc   class   rom   var   

原文地址:http://www.cnblogs.com/zyip/p/6130143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!