Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Gozala/3747168 to your computer and use it in GitHub Desktop.
Save Gozala/3747168 to your computer and use it in GitHub Desktop.
Extend function prototype to run a function as a WebWorker
Function.prototype.runOnBackgroundThread = function (aCallback) {
var _blob = new Blob(['onmessage = '+this.toString()],{"type":"text/javascript"});
var _worker = new Worker((webkitURL.createObjectURL || URL.createObjectURL)(_blob));
_worker.onmessage = aCallback;
_worker.postMessage();
}
var _test = function () {
postMessage((1+1).toString());
}
_test.runOnBackgroundThread(function(e){
alert(e.data);
});
@fydo23
Copy link

fydo23 commented Mar 4, 2014

This is brilliant! Staring it for reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment