Last active
April 22, 2016 17:26
-
-
Save a-x-/0d4e58fcbc2af7fdccd06a9402d1eccb to your computer and use it in GitHub Desktop.
Javascript threads
This file contains hidden or 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
| var workerFunction = function() { | |
| self.onmessage = function(e) { | |
| self.postMessage('msg from worker' + e.data); | |
| self.close(); | |
| }; | |
| }; | |
| var createWorker = fn => { | |
| var workerFunctionBlob = new Blob(['('+fn.toString()+')()'], { type: "text/javascript" }); | |
| var worker = new Worker(URL.createObjectURL(workerFunctionBlob)); | |
| URL.revokeObjectURL(workerFunctionBlob); // free fake blob url | |
| return worker; | |
| }; | |
| var worker = createWorker(workerFunction); | |
| worker.onmessage = function(e) { | |
| console.log("Received: " + e.data); | |
| } | |
| worker.postMessage("hello"); // Start the worker. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment