当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Running a function from a web worker

问题描述:

I have a JS object that has a function which takes another object and an int:

this.foo.func(bar, int);

Is there a way to pass this function (and/or object) to a worker so it can execute it? Or somehow share the scope with the worker?

I'm not sure but I think Transferable Objects won't help me here since none of the objects can be translated to JSON?

网友答案:

A worker cannot run functions that affect the DOM.

You can only (right now) pass strings to a webworker.


What you can do is pass as much information as you can that the webworker can use, and then return what you did to the main window.

网友答案:

You can do this with vkThread plugin.

It looks like this:

function foo(n, m) {
    return n+m;
}

vkthread.exec(foo,     // function to execute in a web worker
              [1, 2],  // arguments for the function
              function(data){ // callback
                             console.log(data); // 3
                           }
);

see details at: http://www.eslinstructor.net/vkthread/

分享给朋友:
您可能感兴趣的文章:
随机阅读: