var blob = new Blob([` self.onmessage = function(e) { postMessage('Msg from worker'); } `]); var blobURL = window.URL.createObjectURL(blob); var worker = new Worker(blobURL); worker.postMessage(null); worker.onmessage = function(e) { console.log(e.data); }
var worker = new Worker('worker.js'); worker.addEventListener('message', function(event) { console.log('message received from worker => ', event.data); }); // listen to error event of worker worker.addEventListener('error', function(event) { console.error('error received from worker => ', event); }); worker.postMessage(null); // worker.terminate(); // When worker needs to be terminated from main thread
self.onmessage = function(event) { var x = 0; for (var i = 0; i < 200000000; i++) { x = x + i; } self.postMessage(x); } // self.close(); // When worker needs to terminate it self