3

I wrote 3 files to 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 i = 0; i < intArray.length; i++){ j = parseInt(i / 10, 0); k = i % 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 is a main html to show.

webworker3.1.js

onmessage = function(event){ var intArray = new Array(100); for(var i = 0; i < 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 is the main thread, I create a child thread in it. webworker3.2.js

onmessage = function(event){ var intArray = JSON.parse(event.data); var returnStr = ''; for(var i = 0; i < intArray.length; i++){ if(parseInt(intArray[i] % 3) == 0){ if('' !== returnStr){ returnStr += ';'; } returnStr += intArray[i]; } } postMessage(returnStr); close(); } 

When I open webworker.html in Chrome, it has a error:

webworker3.1.js:7 Uncaught ReferenceError: Worker is not defined 

I can't find the reason, who can tell me?

1
  • What is the version of your chrome? Commented Jul 2, 2015 at 9:25

1 Answer 1

2

Google Chrome has a known bug about nested workers, probably it hasn't been corrected yet; see this for more infos:

https://code.google.com/p/chromium/issues/detail?id=31666

Sign up to request clarification or add additional context in comments.

1 Comment

Think you, I run it on IE and it ok

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.