javascript - Why Worker is not defined in js -
i wrote 3 files test webworker,
webworker.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>document</title> </head> <body> <table id="table"></table> <script> // var worker = new worker('webworker3.1.js'); var worker = new worker('webworker3.1.js'); worker.postmessage(''); worker.onmessage = function(event){ console.log(event.data); if(event.data != ''){ var j, k, tr, td, intarray = event.data.split(';'), table = document.getelementbyid('table'); for(var = 0; < intarray.length; i++){ j = parseint(i / 10, 0); k = % 10; if(k == 0){ tr = document.createelement('tr'); tr.id = 'tr' + j; table.appendchild(tr); }else{ tr = document.getelementbyid('tr' + j); } td = document.createelement('td'); tr.appendchild(td); td.innerhtml = intarray[j * 10 + k]; td.style.backgroundcolor = 'blue'; td.style.color = 'white'; td.width = '30'; } } }; </script> </body> </html>
this main html show.
webworker3.1.js
onmessage = function(event){ var intarray = new array(100); for(var = 0; < 100; i++){ intarray[i] = parseint(math.random() * 100); } var worker = new worker('webworker3.2.js'); worker.postmessage(json.stringify(intarray)); worker.onmessage = function(event){ postmessage(event.data); } }
this main thread, create child thread in it. webworker3.2.js
onmessage = function(event){ var intarray = json.parse(event.data); var returnstr = ''; for(var = 0; < intarray.length; i++){ if(parseint(intarray[i] % 3) == 0){ if('' !== returnstr){ returnstr += ';'; } returnstr += intarray[i]; } } postmessage(returnstr); close(); }
when open webworker.html in chrome, has error:
webworker3.1.js:7 uncaught referenceerror: worker not defined
i can't find reason, can tell me?
google chrome has known bug nested workers, hasn't been corrected yet; see more infos:
Comments
Post a Comment