工作线程中的 window.alert

Don*_* Ch 5 javascript web-worker

如果我在 webworker 客户端上放置 window.alert,那么后台工作人员就会停止工作。为什么会这样呢?

即调用者:

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");
Run Code Online (Sandbox Code Playgroud)

客户端(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");
}
Run Code Online (Sandbox Code Playgroud)

fms*_*msf 1

您是否注意到警报会冻结 JavaScript 引擎,直到用户单击“确定”。

如果您不想让它冻结,请不要使用警报。

使用 firebug 进行调试:

console.log("bla bla bla");
Run Code Online (Sandbox Code Playgroud)

对于非锁定弹出窗口:

制作一个隐藏的div,上面有一个确定按钮。当要显示弹出窗口时。让 div 可见。当用户单击“确定”时将其隐藏。

我建议你不要使用弹出窗口。它还打破了屏幕后面用户的“工作流程”(意味着用户的注意力):)