小编hel*_*ope的帖子

Web Worker被Chrome中的主线程阻止

我有一个Web Worker.我希望用它定期发送网络请求.我特别想要的一件事就是即使主要的JS执行线程被阻塞(例如通过window.alert)也要发出这些请求.我正在使用Chrome 38.

但是,当我尝试在worker中发出网络请求时,UI线程似乎阻止了请求.这是一个人为的例子来说明问题:

base.js:

var worker = new Worker("/worker.js");

setTimeout(function() {
    console.log("begin blocking");
    var startDt = new Date();
    var blockPeriod = 5000;
    var a;
    // Obviously we'd never actually do this, but this while loop
    // is a convenient way to create the problem case (a blocked main
    // thread).
    while ((new Date() - startDt) < blockPeriod) {
        a = 0;
    }
    console.log("stop blocking");
}, 3000);
Run Code Online (Sandbox Code Playgroud)

worker.js:

var requestInterval = 1000;

var sendRequest = function() {
    console.log("Send …
Run Code Online (Sandbox Code Playgroud)

javascript multithreading google-chrome xmlhttprequest web-worker

13
推荐指数
1
解决办法
3139
查看次数