web worker demo

2104 단어
 
var worker = new Worker('/jsteststatic/static/worker.js') // , 
worker.postMessage('Hello World')
worker.postMessage({method: 'echo', args: ['Work']})
worker.onmessage = function (event) {
  console.log('Received message ' + event.data)
  // worker.terminate()  //  web worker  
} 

worker.js 중
var self = this
self.addEventListener('message', function (e) {
  self.postMessage('You said: ' + e.data)
}, false)

통신 완료.
두 번째 로컬 웹 워커
"worker"</span> <span class="hljs-built_in">type</span>=<span class="hljs-string">"app/worker"</span>>
  addEventListener(<span class="hljs-string">'message'</span>, <span class="hljs-function"><span class="hljs-title">function</span></span> () {
    postMessage(<span class="hljs-string">'some message'</span>);
  }, <span class="hljs-literal">false</span>);


  var blob = new Blob([document.querySelector(<span class="hljs-string">'#worker'</span>).textContent]);
  var url = window.URL.createObjectURL(blob);
  var worker = new Worker(url);
  console.log(<span class="hljs-string">'w:'</span>+worker)
  worker.postMessage(<span class="hljs-string">'Hello index'</span>)
  worker.onmessage = <span class="hljs-keyword">function</span> (e) {
    console.log(e.data)
  };

통신 완료.
참조 링크:http://www.ruanyifeng.com/blog/2018/07/web-worker.html

좋은 웹페이지 즐겨찾기