Skip to content

Instantly share code, notes, and snippets.

@jay3sh
Created August 16, 2011 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jay3sh/1149347 to your computer and use it in GitHub Desktop.
Save jay3sh/1149347 to your computer and use it in GitHub Desktop.
WebWorker postMessage for debug
//
// In the Main thread
//
var worker = new Worker('/path/of/webworker/code.js')
worker.onmessage = function (e) {
var result = JSON.parse(e.data);
if(result.type == 'debug') {
console.log(result.msg);
} else if(result.type == 'response') {
// ... use result.answer ...
}
}
//
// In the WebWorker
//
function debug(msg) {
postMessage(JSON.stringify({type:'debug',msg:msg}));
}
onmessage = function (e) {
var inputData = e.data;
// work on input data
debug('Working OK');
// work some more
// ...
postMessage(JSON.stringify({type:'response', answer:42}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment