Why can't Web Worker call a function directly?

前端 未结 7 1122
借酒劲吻你
借酒劲吻你 2021-02-01 03:15

We can use the web worker in HTML5 like this:

var worker = new Worker(\'worker.js\');

but why can\'t we call a function like this?



        
7条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 03:27

    Just use my tiny plugin https://github.com/zevero/worker-create

    and do

    var worker_url = Worker.create(function(e){
      self.postMessage('Example post from Worker'); //your code here
    });
    var worker = new Worker(worker_url);
    

提交回复
热议问题