Worker Class
The Web Workers specification defines an API for spawning background scripts in your web application. Web Workers allow you to do things like fire up long-running scripts to handle computationally intensive tasks, but without blocking the UI or other scripts to handle user interactions. Because Workers aren't available on all browsers, we provide a helpful polyfill for backward compatibility.
var workerCode = "this.initialVariable = 10;" +
"this.onmessage = function(event)" +
"{" +
"var data = event.data;" +
"var returnVal = this.initialVariable + data.addValue;" +
"this.postMessage(returnVal);" +
"};";
// Create the worker
var worker = cloudkid.Worker.init(workerCode);
worker.onmessage = function(e) {
// e.data is the returnVal
};
// Start the worker.
worker.postMessage();
Item Index
Methods
- init static
Methods
init
(
FallbackWorker | window.Worker
static
-
codeString
Initialize the worker, this is how you create a Worker or FallbackWorker object.
Parameters:
-
codeString
StringThe code in string form to make the worker from. As a string, fallback support is easier.
Returns:
FallbackWorker | window.Worker:
Either a Web Worker or a fallback with the same API to use.