HTML 5 webworkers with multiple arguments

女生的网名这么多〃 提交于 2019-12-19 19:52:54

问题


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.


回答1:


Post like this:

w.postMessage({user:username,url:server_url})

on message event do:

onmessage = function (e,f) {
    username = e.data.username;
    server_url = e.data.url;
}


来源:https://stackoverflow.com/questions/35271039/html-5-webworkers-with-multiple-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!