Skip to content

Instantly share code, notes, and snippets.

@vermie
Created August 15, 2014 17:35
Show Gist options
  • Save vermie/9818c441d261ccf6219a to your computer and use it in GitHub Desktop.
Save vermie/9818c441d261ccf6219a to your computer and use it in GitHub Desktop.
create worker form <script> blob url
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