网络工作者未运行
我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在收到消息之前,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.您确定您的浏览器支持这一特定的 HTML5 功能吗?
本文提供了多种方法来测试每个功能的支持。 Worker 的测试是
编辑:据我所知,您的代码有问题,或者找不到文件。您的代码看起来很像此示例,除了 .js 文件代码是这样的,带有
self
:您应该很容易尝试一下,看看它是否是这里缺少的部分。
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
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
:It should be easy enough for you to try that and see if it's the missing piece here.
您应该记住的一件主要事情如果您在同源上运行脚本并使用 chrome,则应该使用标志 --allow-file-access-from-files 启动 chrome,或者您应该在以下位置运行应用程序本地服务器。
查看代码
现在您将得到工作人员的响应。 “..”我怀疑路径错误。
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
Now you will get The response from the worker. ".." I suspect path is wrong.