Web Worker不适用于chrome上的localhost,但适用于chrome开发人员上的localhost

时间:2018-09-09 06:26:41

标签: javascript web-worker

我在Google上得到了一个Web Worker(下面的代码)的示例。 我知道

  

从本地文件运行脚本时,Chrome不允许您加载网络工作者。

因此,我在运行Node.js http服务器的本地主机上尝试了它,但是它在“ Chrome开发者”中可以运行,尽管在“ Chrome”中不起作用。

    //main.js
    var w; //the variable of object for web worker

    function startWorker() {
        if (typeof (Worker) != "undefined") //checking if browser supports web worker
        {
            if (typeof (w) == "undefined") {
                w = new Worker("js/worker.js");
            }
            w.onmessage = function (e) {
                document.getElementById('result').innerHTML = e.data;
            };
        }
        else {
            document.getElementById('result').innerHTML = "Your browser doesnot support HTML5 Web Worker! :)"; // or display the message that web worker is not supported!
        }
    }

    function endWorker() {
        w.terminate();
    }

    //worker.js
    var i = 0;

    function timedCount() {
        i = i + 1;
        postMessage(i);
        setTimeout("timedCount()", 500);
    }

    timedCount();

请帮助我理解。

0 个答案:

没有答案