开发者

javascript Web worker - pass data to page thread

If I have a var on my main page, and have a worker thread trying to set this var, is there a way the page can开发者_如何转开发 access it? Assuming everything is synchronized?

var routeWorker = new Worker('getroute.js');
var checkPatrolRouteFoundTimer;
var rw_resultRoute;
var routeFound = false;

routeWorker.onmessage = function(e) {
        rw_resultRoute = e.data.route;
        routeFound = true;
}

function checkPatrolReady() {
 if(!routeFound)
     checkPatrolRouteFoundTimer = setTimeout("checkPatrolReady()", 1000);
}

function ForcePatrol(index) {
 routeWorker.postMessage(index);
    checkPatrolReady();
    ...
    //do work on route
    ...
}

in this case, the var I'm talking about is rw_resultRoute, and I can see it get set correctly when debugging. But the only thing is that it's set in the worker thread, not in the page thread.

I flow through the ForcePatrol() method the way i'm expecting to, and it looks like the rw_resultRoute is being set, since routeFound evaluates to true after the worker finishes.

Technically, it doesn't make sense, since routeFound can be set by the worker and read by the page thread, but rw_resultRoute can only be accessed by the worker.

I truly hope this is possible, otherwise I don't see a purpose for worker threads other than showing alert() messages and updating page HTML.


I truly hope this is possible, otherwise I don't see a purpose for worker threads other than showing alert() messages and updating page HTML.

It is meant to handle processing that would normally lock up the browser. Great for crunching numbers for canvas and running hashing.

in this case, the var I'm talking about is rw_resultRoute, and I can see it get set correctly when debugging. But the only thing is that it's set in the worker thread, not in the page thread.

The worker is separate from the page that spawns it. Only way to pass data is through messaging. You need to send the data with postMessage and have the onMessage handle the result. If you are handling different things, set up a switch statement to handle the different message types.


I solved the problem. There was some synchronization I wasn't doing correctly. I was using the setTimeout in the wrong way.

var routeWorker = new Worker('getroute.js');
var checkPatrolRouteFoundTimer;
var rw_resultRoute;
var routeFound = false;

routeWorker.onmessage = function(e) {
    rw_resultRoute = e.data.route;
    routeFound = true;
}

function checkPatrolReady() {
    if(routeFound) {
        ...
        //do work on route
        ...
        clearInterval(checkPatrolRouteFoundTimer);
    } else {
        // do any maint here?
    }
}

function ForcePatrol(index) {
    routeWorker.postMessage(index);
    checkPatrolRouteFoundTimer = setInterval("checkPatrolReady()", 1000);
}

Any call to setTimeout/setInterval will flow through, and in the first example i was using setTimeout instead of setInterval.

In the new way, calling ForcePatrol will setup the timer, and checkPatrolReady() will evaluate the flag, doing the work and clearing the timer if it is true.

So there is indeed nothing fancy in getting the results from web workers, but I was essentially creating a race condition with the worker results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜