网络工作者未运行

发布于 2024-11-13 12:18:05 字数 483 浏览 5 评论 0原文

我有以下代码:

var stressWorker = new Worker("./test/webworkers/worker.js");
stressWorker.onmessage = function(event){
    alert(event.data);
};
stressWorker.onerror = function(event){
    throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
};

worker.js:

onmessage = function(e){
   postMessage("test");
}

该脚本找到“worker.js”文件,但实际上并没有运行它。我做错了什么?

附言。我使用 wamp 托管这两个脚本,并且使用 chrome

I have the following code:

var stressWorker = new Worker("./test/webworkers/worker.js");
stressWorker.onmessage = function(event){
    alert(event.data);
};
stressWorker.onerror = function(event){
    throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
};

worker.js:

onmessage = function(e){
   postMessage("test");
}

The script finds the 'worker.js' file but it doesn't actually run it. What am I doing wrong?

PS. I'm hosting both scripts using wamp and I'm using chrome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十秒萌定你 2024-11-20 12:18:06

在收到消息之前,worker.js 不会执行任何操作。我看不到你向哪里发送消息。你需要类似 stressWorker.postMessage(...) 的东西。

worker.js won't do anything until it receives a message. I can't see where you are sending it a message. You need something like stressWorker.postMessage(...) somewhere.

甜妞爱困 2024-11-20 12:18:06

您确定您的浏览器支持这一特定的 HTML5 功能吗?

本文提供了多种方法来测试每个功能的支持。 Worker 的测试是

return !!window.Worker;

编辑:据我所知,您的代码有问题,或者找不到文件。您的代码看起来很像此示例,除了 .js 文件代码是这样的,带有 self

self.onmessage = function(e) {  
    self.postMessage("Hello " + e.data);  
};  

您应该很容易尝试一下,看看它是否是这里缺少的部分。

Are you sure your browser supports this particular HTML5 feature?

This article has many ways to test for support of each feature. The test for Worker is

return !!window.Worker;

Edit: As I see it, there's either a problem with your code or it can't find the file. Your code looks a lot like this example except there the .js file code is like this, with the self:

self.onmessage = function(e) {  
    self.postMessage("Hello " + e.data);  
};  

It should be easy enough for you to try that and see if it's the missing piece here.

无声无音无过去 2024-11-20 12:18:06

您应该记住的一件主要事情如果您在同源上运行脚本并使用 chrome,则应该使用标志 --allow-file-access-from-files 启动 chrome,或者您应该在以下位置运行应用程序本地服务器

查看代码

 var stressWorker = new Worker("../test/webworkers/worker.js");
    stressWorker.onmessage = function(event){
        alert(event.data);
    };
    stressWorker.postMessage("Hello there");
    stressWorker.onerror = function(event){
        throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
    };

现在您将得到工作人员的响应。 “..”我怀疑路径错误。

One main thing You should remember If you are running scripts on same origin and using chrome, you should start the chrome with the flag --allow-file-access-from-files or You should run app on local server.

See the code

 var stressWorker = new Worker("../test/webworkers/worker.js");
    stressWorker.onmessage = function(event){
        alert(event.data);
    };
    stressWorker.postMessage("Hello there");
    stressWorker.onerror = function(event){
        throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
    };

Now you will get The response from the worker. ".." I suspect path is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文