Forked from jameswomack/javascript_background_thread.js
Created
September 18, 2012 20:04
-
-
Save ashee/3745501 to your computer and use it in GitHub Desktop.
Extend function prototype to run a function as a WebWorker
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.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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment