-
-
Save CHANG-CHING-CHUNG/1001c61ba760e85cd9dc378d38956462 to your computer and use it in GitHub Desktop.
Web Worker via blob URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<body> | |
<span id="output"></span> | |
</body> | |
<script> | |
(function () { | |
var workerBlob = new Blob( | |
[workerRunner.toString().replace(/^function .+\{?|\}$/g, '')], | |
{ type:'text/javascript' } | |
); | |
var workerBlobUrl = URL.createObjectURL(workerBlob); | |
var worker = new Worker(workerBlobUrl); | |
worker.onmessage = function(event) { | |
output.textContent = 'Output is: ' + event.data; | |
}; | |
worker.postMessage('foo'); | |
function workerRunner() { | |
self.onmessage = function(event) { | |
self.postMessage('launched worker via blob URL!'); | |
} | |
}; | |
})(); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment