Last active
August 29, 2015 14:03
-
-
Save christophemarois/0b9332d62ca140b2ba35 to your computer and use it in GitHub Desktop.
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
var createWorker = function(workerFn) { | |
// Convert function to string, and get rid of the | |
// wrapping `function(){}` | |
var workerFnContents = workerFn.toString() | |
.replace(/^\s*function[^(]*\([^)]*\)\s*{/igm, '') | |
.replace(/}\s*$/, ''); | |
var blob = new Blob([workerFnContents], {type: 'application/javascript'}); | |
return new Worker(window.URL.createObjectURL(blob)); | |
}; | |
var myWorker = createWorker(function(){ | |
postMessage('Worker initialized'); | |
onmessage = function (message) { | |
postMessage("Hi " + message.data); | |
}; | |
}); | |
myWorker.onmessage = function (message) { | |
console.log("Worker said : " + message.data); | |
}; | |
myWorker.postMessage('Nadim'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment