5

What if I want to put a web worker on pause if I cannot proceed processing data, and try a second later? Can I do that in this manner inside a web worker?

var doStuff = function() {
    if( databaseBusy() ) {
        setTimeout(doStuff, 1000);
    } else {
        doStuffIndeed();
    }
}

I'm getting Uncaught RangeError: Maximum call stack size exceeded and something tells me it's because of the code above.

5
  • Why not use setInterval() instead if you want it to repeat? Commented Feb 26, 2014 at 13:40
  • I'd like to stop it when the database stop being busy. Commented Feb 26, 2014 at 13:41
  • 1
    @MishaSlyusarev Then use clearInterval() after your condition is met. Commented Feb 26, 2014 at 13:43
  • 1
    Are you sure the problem is the timeout for the stack size? Are you using any recursion in the webworker code? Commented Feb 26, 2014 at 13:46
  • Not sure that's the case, just wanted to figure out the proper way of using setTimeout from web workers. Commented Feb 26, 2014 at 13:51

1 Answer 1

4

If by "pause" you mean "further calls to worker.postMessage() will get queued up and not processed by the worker", then no, you cannot use setTimeout() for this. setTimeout() is not a busywait, but rather an in-thread mechanism for delaying work until a later scheduled time. The web worker will still receive a new onmessage event from the main queue as soon as it is posted.

What you can do, however, is queue them up manually and use setTimeout to try to process them later. For example:

worker.js

var workQueue = [];
addEventListener('message',function(evt){
  workQueue.push(evt.data);
  doStuff();
},false);

function doStuff(){
  while (!databaseBusy()) doStuffIndeed(workQueue.shift());
  if (workQueue.length) setTimeout(doStuff,1000);
}
  • Each time a message is received the worker puts it into a queue and calls tryToProcess.
  • tryToProcess pulls messages from the queue one at a time as long as the database is available.
  • If the database isn't available and there are messages left, it tries again in 1 second.
Sign up to request clarification or add additional context in comments.

3 Comments

Well my worker is going to receive a single message, get data from the database, process loaded data, and post a result back to the main thread. I just want to pause until database available for reading.
@MishaSlyusarev Then this solution will work well for you, even if the queue is only ever empty or has one item. Are you sure that a second request for data won't come in while you are waiting to process the first?
However, if you truly will only ever have one request in flight, then your original solution should work just fine. It will not cause a tail call or use any recursion, provided you have it as you have written it and NOT setTimeout(doStuff(),1000) (with the parens on the function call).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.