Created
August 15, 2014 17:35
-
-
Save vermie/9818c441d261ccf6219a to your computer and use it in GitHub Desktop.
create worker form <script> 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
function makeWorker(fn, args, callback) { | |
var fnString = 'self.addEventListener("message", function (e) {self.postMessage((' + fn.toString() + ').apply(this, e.data))});', | |
blob = new Blob([fnString], { type: 'text/javascript' }), | |
url = URL.createObjectURL(blob), | |
worker = new Worker(url); | |
worker.postMessage(args); | |
worker.addEventListener('message', function (e) { | |
URL.revokeObjectURL(url); | |
callback(e.data); | |
}); | |
return worker; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment