此 WebWorker 出了什么问题(没有错误,但未到达 console.log)
我有以下代码,试图测试 WebWorkers。我有一个如下所示的index.html文件:
<html>
<head></head>
<body>
<script type='text/javascript'>
var worker = new Worker('./myworker.js');
console.log('after creation');
worker.addEventListener('message', function(msg){
console.log(msg);
});
worker.postMessage();
</script>
</body>
</html>
myworker.js(与index.html位于同一目录中)的内容是:
this.onmessage = function(){
postMessage('got the msg, thanks');
};
当我加载index.html(在Chrome 14中)时,“创建后”控制台.log 永远不会发生。也没有其他任何东西。 Console.logs 在 new Worker() 创建之前发生,但之后似乎没有发生任何事情。
I have the following code, trying to test out WebWorkers. I have an index.html file that looks like this:
<html>
<head></head>
<body>
<script type='text/javascript'>
var worker = new Worker('./myworker.js');
console.log('after creation');
worker.addEventListener('message', function(msg){
console.log(msg);
});
worker.postMessage();
</script>
</body>
</html>
The contents of myworker.js (which resides in the same directory as index.html) is:
this.onmessage = function(){
postMessage('got the msg, thanks');
};
When I load index.html (in Chrome 14), the 'after creation' console.log never happens. Nor anything else. Console.logs happen before the new Worker() creation, but nothing after seems to happen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,黄油我的饼干,显然 WebWorkers 在本地加载时(例如从 file://)无法工作。
来源:http://www.html5rocks.com/en/tutorials/workers/basics/ (内容底部)
Well butter my biscuit, apparently WebWorkers do not work when loaded locally (e.g. from file://).
source: http://www.html5rocks.com/en/tutorials/workers/basics/ (bottom of content)
如果您正在 Chrome 中测试应用程序,则应使用
--allow-file-access-from-files
启动 chrome 或使用本地服务器测试应用程序If you are testing app in chrome you should start the chrome with
--allow-file-access-from-files
or test app using Local server