When I tried to play with the Web Workers feature in HTML5, my firefox worked happily, but Chrome complains that:
Uncaught TypeError: Unable to call postMessage method from undefinedxstartWorkerworker.html: 7 (anonymous function) worker.html: 1 onclickworker.html: 2
worker.html
<button onclick="xstartWorker()">Start worker</button> <output id="result"></output> <script> function xstartWorker() { worker.postMessage({'cmd': 'startWorker', 'msg': 'Start now!'}); } var worker = new Worker('worker.js'); worker.addEventListener('message', function(e) { document.getElementById('result').textContent = e.data; } , false); </script>
worker.js
self.addEventListener('message', function(e) { var data = e.data; switch (data.cmd) { case 'startWorker': self.postMessage('worker thread start now:' + data.msg); break; default: self.postMessage('default'); } } , false);
What can I do to make it work in chrome?
By the way, when I tried the sample at http://playground.html5rocks.com/#inline_workers and this time chrome works, but firefox complains that
Error: working undefined Source file: http://playground.html5rocks.com/ Line: 39
javascript html5 firefox google-chrome
janetsmith Jul 25 2018-11-11T00: 00Z
source share