具有多个参数的HTML 5网络工作者 [英] HTML 5 webworkers with multiple arguments
问题描述
我刚接触HTML5网络工作者,现在我想将多个参数传递给我的工作人员.
I just got into HTML5 webworkers and now I want to pass multiple arguments to my worker.
我的页面中有此内容:
var username = document.getElementById("username").value;
var server_url = 'localhost';
w.postMessage(username,server_url);
这是我的工作人员中的
var username = '';
var server_url = '';
onmessage = function (e,f) {
username = e.data;
server_url = f.data;
}
console.log(username);
console.log(server_url);
,当我打开它时,在浏览器中调用工作程序的页面:
Uncaught TypeError: Failed to execute 'postMessage' on 'Worker': The 2nd argument is neither an array, nor does it have indexed properties.
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.
我要做的就是将username
和server_url
传递给工作人员
我知道在示例中我对server_url
进行了硬编码,但是在实际脚本中,它是动态的.
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.
推荐答案
像这样发布:
w.postMessage({
user: username,
url: server_url
})
发生消息事件时:
onmessage = function (e) {
username = e.data.user;
server_url = e.data.url;
}
这篇关于具有多个参数的HTML 5网络工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!