最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Calling functions in Web Workers - Stack Overflow

programmeradmin7浏览0评论

Ok this is kinda difficult to explain but here goes, I am using web workers and I have functions defined in my web workers script, and I would like to call a particular function passing in another function as arguments, the problem is I am trying to do this from my main script using worker.postMessage. It might be clearer if I post some code...

This is from my main script:

worker.postMessage({'cmd': 'register', 'funcName': 'someFunction'});

This is from the worker script:

self.addEventListener('message', function(e) {
    var data = e.data;

switch (data.cmd) {
    case 'register':
        registerEvent(data.funcName);
        break;
    case 'unregister':
        break;
    default:
        self.postMessage('Unknown mand: ' + data.msg);
    };
}, false);

function someFunction() {

}

function registerEvent(someFunction) {

}

So I know this code is wrong and won't work, but I guess you have an idea of what I'm trying to achieve.

Ok this is kinda difficult to explain but here goes, I am using web workers and I have functions defined in my web workers script, and I would like to call a particular function passing in another function as arguments, the problem is I am trying to do this from my main script using worker.postMessage. It might be clearer if I post some code...

This is from my main script:

worker.postMessage({'cmd': 'register', 'funcName': 'someFunction'});

This is from the worker script:

self.addEventListener('message', function(e) {
    var data = e.data;

switch (data.cmd) {
    case 'register':
        registerEvent(data.funcName);
        break;
    case 'unregister':
        break;
    default:
        self.postMessage('Unknown mand: ' + data.msg);
    };
}, false);

function someFunction() {

}

function registerEvent(someFunction) {

}

So I know this code is wrong and won't work, but I guess you have an idea of what I'm trying to achieve.

Share Improve this question asked Aug 1, 2013 at 16:34 JackJack 4372 gold badges8 silver badges19 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I would do something like this:

self.addEventListener('message', function(e) {
    var data = e.data;

    switch (data.cmd) {
        case 'register':
            registerEvent(data.funcName);
            break;
        case 'unregister':
            break;
        default:
            self.postMessage('Unknown mand: ' + data.msg);
    };
}, false);

self.someFunction = function() {}

function registerEvent(someFunctionName) {
    self[someFunctionName]();
}

or create a separate object with the function, but make sure it is a method of an object, then it bees easy to call like that.

I would remend you to look at the vkTread plugin. This plugin allows you to execute any function of your code in a thread.

http://www.eslinstructor/vkthread/

Best regards,

--Vadim

发布评论

评论列表(0)

  1. 暂无评论