webworker消息传递不起作用

时间:2017-04-25 19:37:41

标签: javascript parallel-processing web-worker

以下是主要js和worker js文件的代码:

main.js
function main(){
        var loadShWorker = new Worker('worker.js');
        var loadShWorkerMsgEvntListnr = function(e) {
            console.log("reached main");
          console.log('Worker said: ', e.data);
        }
        // loadShWorker.port.start();
        loadShWorker.removeEventListener('onmessage',loadShWorkerMsgEvntListnr);
        loadShWorker.addEventListener('onmessage', loadShWorkerMsgEvntListnr, false);
        // loadShWorker.port.start();
      // loadShWorker.port.postMessage('Hello World');
        loadShWorker.postMessage('Hello World');
    }


worker.js
console.log("reached worker top");
self.addEventListener('onmessage', function(e) {
  console.log("reached worker");
  self.postMessage(e.data);
  console.log("posted msg from worker");
}, false);

此处控件未到达主js文件。我做了什么不同的

0 个答案:

没有答案