Search code examples
javascriptweb-worker

window.alert in worker thread


If I put a window.alert on a webworker client, then the background worker stops working. Why is this so?

i.e. The caller:

var worker = new Worker("worker.js");
// Watch for messages from the worker
worker.onmessage = function(e){
  // The message from the client:
  e.data
};
worker.postMessage("start");

The client (worker.js)

onmessage = function(e){
  if ( e.data === "start" ) {
    // Do some computation
    done()
  }
};

function done(){
  alert('don');  // ===> This kills the worker.
  // Send back the results to the parent page
  postMessage("done");
}

Solution

  • Has you have noticed the alert freezes the javascript engine until the user clicks OK.

    If you don't want it to freeze don't use alerts.

    For debuging with firebug:

    console.log("bla bla bla");
    

    For non locking popups:

    make a hidden div with an ok button on it. When the popup is to be shown. Put the div visible. When the user clicks the "ok" hide it.

    I would advise you not to use popups. It also breaks the "work flow" (meaning the concentration of the user) of the user behind the screen :)