Skip to content

Instantly share code, notes, and snippets.

@uupaa
Last active August 27, 2018 00:41
Show Gist options
  • Save uupaa/1bc6ccba7281f7d3231ba04686e2a629 to your computer and use it in GitHub Desktop.
Save uupaa/1bc6ccba7281f7d3231ba04686e2a629 to your computer and use it in GitHub Desktop.
WebWorker make by a string.
<script>
const WORKER_RESOURCE = `
self.onmessage = (event) => {
debugger;
postMessage(event.data + " worker"); // hello worker
}`;
const blob = new Blob( [ WORKER_RESOURCE ], { type: "application/javascript" } );
const blobURL = URL.createObjectURL(blob); // -> URL.revokeObjectURL(blobURL);
const worker = new Worker(blobURL);
worker.onmessage = (event) => { // @arg EventObject - { data: "hello worker" }
alert('resp: ' + event.data);
};
worker.postMessage('hello');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment