Skip to content

Instantly share code, notes, and snippets.

@devdays
Created November 30, 2014 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devdays/8f79a8f672ab886af291 to your computer and use it in GitHub Desktop.
Save devdays/8f79a8f672ab886af291 to your computer and use it in GitHub Desktop.
Web Worker Two Way Communication JavaScript
self.addEventListener('message', function (e) {
var data = e.data;
switch (data.cmd) {
case 'start':
self.postMessage('My worker has: ' + data.msg);
break;
case 'stop':
self.postMessage('My worker has stopped: ' + data.msg + '. (buttons will no longer work)');
self.close(); // Terminates the worker.
break;
default:
self.postMessage('Huh? ' + data.msg);
};
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment