Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 25, 2015 00:39
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 creationix/75cb3d540418980f32a8 to your computer and use it in GitHub Desktop.
Save creationix/75cb3d540418980f32a8 to your computer and use it in GitHub Desktop.
var blob = new Blob([
"onmessage = function(e) { postMessage('msg from worker'); }"],
{type:"application/javascript"}
);
// Obtain a blob URL reference to our worker 'file'.
var blobURL = window.URL.createObjectURL(blob);
var worker = new Worker(blobURL);
worker.onmessage = function(e) {
var p = document.createElement('p');
p.textContent = e.data;
document.body.appendChild(p);
console.log(e.data);
// e.data == 'msg from worker'
};
worker.postMessage(""); // Start the worker.
@creationix
Copy link
Author

Read the code again, all the worker is doing is onmessage = function(e) { postMessage('msg from worker'); }". The dom manipulation is done in the main context where it belongs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment