Created
June 14, 2011 09:02
-
-
Save davidmz/1024547 to your computer and use it in GitHub Desktop.
Создание Web Worker-а без использования внешнего JS-файла. Работает ТОЛЬКО в Google Chrome (остальные браузеры не поддерживают BlobBuilder).
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
/** | |
* Создание Web Worker-а без использования внешнего JS-файла. | |
* Работает ТОЛЬКО в Google Chrome (остальные браузеры не поддерживают BlobBuilder) | |
*/ | |
function InlineWorker(code) { | |
var URL = (self.URL || self.webkitURL), BlobBuilder = (self.BlobBuilder || self.WebKitBlobBuilder); | |
var bb = new BlobBuilder(); | |
bb.append("(" + code.toString() + ")();"); | |
var url = URL.createObjectURL(bb.getBlob("application/javascript")); | |
var worker = new Worker(url); | |
URL.revokeObjectURL(url); | |
return worker; | |
} | |
var worker = new InlineWorker(function() { | |
self.postMessage("hi!"); | |
}); | |
worker.onmessage = function(e) { alert('Worker said: ', e.data); }; | |
worker.postMessage(""); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment