admin管理员组文章数量:1297106
I just got into HTML5 webworkers and now I want to pass multiple arguments to my worker.
I have this in my page:
var username = document.getElementById("username").value;
var server_url = 'localhost';
w.postMessage(username,server_url);
and this in my worker:
var username = '';
var server_url = '';
onmessage = function (e,f) {
username = e.data;
server_url = f.data;
}
console.log(username);
console.log(server_url);
and when I open it the page which calls the worker in the browser:
Uncaught TypeError: Failed to execute 'postMessage' on 'Worker': The 2nd argument is neither an array, nor does it have indexed properties.
all I want to do is have the username
and server_url
getting passed to the worker
I know that in the examples I hardcoded the server_url
, but in the real script, it's dynamic.
Please don't just say: change this, do that, but provide me with code so I can see how it should be done rather than still having to figure it out myself.
I just got into HTML5 webworkers and now I want to pass multiple arguments to my worker.
I have this in my page:
var username = document.getElementById("username").value;
var server_url = 'localhost';
w.postMessage(username,server_url);
and this in my worker:
var username = '';
var server_url = '';
onmessage = function (e,f) {
username = e.data;
server_url = f.data;
}
console.log(username);
console.log(server_url);
and when I open it the page which calls the worker in the browser:
Uncaught TypeError: Failed to execute 'postMessage' on 'Worker': The 2nd argument is neither an array, nor does it have indexed properties.
all I want to do is have the username
and server_url
getting passed to the worker
I know that in the examples I hardcoded the server_url
, but in the real script, it's dynamic.
Please don't just say: change this, do that, but provide me with code so I can see how it should be done rather than still having to figure it out myself.
Share Improve this question asked Feb 8, 2016 at 13:37 Finlay RoelofsFinlay Roelofs 5706 silver badges22 bronze badges 2- pass as many arguments in a single json like this w.postMessage({user:username,url:server_url}); – Cyril Cherian Commented Feb 8, 2016 at 13:41
- hmm... I see. and then how do I read them in the worker? – Finlay Roelofs Commented Feb 8, 2016 at 13:43
1 Answer
Reset to default 11Post like this:
w.postMessage({
user: username,
url: server_url
})
on message event do:
onmessage = function (e) {
username = e.data.user;
server_url = e.data.url;
}
本文标签: javascriptHTML 5 webworkers with multiple argumentsStack Overflow
版权声明:本文标题:javascript - HTML 5 webworkers with multiple arguments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741649299a2390375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论