HTML5 - Web Worker
to solve this problem, by using techniques like setTimeout(); setInterval(), XMLHttpRequest.
all of these features run asynchronously, Asynchronous events are processed after the current executing script has yielded.
Uses: more CPU intensive tasks
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.
Web Workers run in an isolated thread, the code that they execute needs to be contained in a separate file, the first thing to do is create a new Worker object in your main page.
var worker = new Worker('task.js');
the browser will spawn a new worker thread, which is downloaded asynchronously.
The worker will not begin until the file has completely downloaded and executed.
If the path to your worker returns an 404, the worker will fail silently.
After creating the worker, start it by calling the postMessage() method
worker.postMessage(); : Communication between a work and its parent page.
postMessage() can accept either a string or JSON object.
stop a worker: worker.terminate() //main page.
self.close() // worker itself.
Features Available to Workers
The navigator object.
The location object (read-only).
The Application Cache.
Importing external scripts using the importScripts() method.
The DOM (it's not thread-safe).
The window object.
The document object.
The parent object.
you can "inline" your worker in the same HTML file as your main logic by creating a URL handle to the worker code as a string
<< Back to the blog