In the past when executing processor intensive JavaScript, browsers would often tend to freeze until the processing had been completed and the results were returned. With the advent of HTML5 Web Workers, you can now execute your processor intensive JavaScript code as a background process that will not affect the performance of the active document. Users will be able to continue using the site as intended as they wait for the Web Worker to complete its jobs in the background.
To easily check if a user's browser has support for HTML5 Web Workers, we can check if the type of the Worker
object is undefined or not:
if(typeof(Worker) == "undefined") { // This browser doesn't support Web Workers... }
Depending on whether the browser supports the usage of Web Workers, we can easily create a new worker at anytime by instantiating a new Worker
object and a reference to its JavaScript source:
worker = new Worker("worker.js");
In the preceding example, we created a new worker and referenced...