Why can't Web Worker call a function directly?

前端 未结 7 1039
借酒劲吻你
借酒劲吻你 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:42

    This answer might be a bit late, but I wrote a library to simplify the usage of web workers and it might suit OP's need. Check it out: https://github.com/derekchiang/simple-worker

    It allows you to do something like:

    SimpleWorker.run({
      func: intensiveFunction,
      args: [123456],
      success: function(res) {
        // do whatever you want
      },
      error: function(err) {
        // do whatever you want
      }
    })
    

提交回复
热议问题