What Is HTML Web Workers API

A HTML Web Workers specialist is a JavaScript running behind the scenes, without influencing the exhibition of the page.

What is a Web Worker?

When executing scripts in a HTML page, the page becomes lethargic until the content is done.

A web specialist is a JavaScript that runs behind the scenes, freely of different contents, without influencing the presentation of the page. You can keep on doing anything you desire: clicking, choosing things, and so on, while the web specialist runs behind the scenes.

Browser Support

The numbers in the table determine the principal program form that completely support Web Workers.

Web Workers4.

HTML Web Workers Example

The model beneath makes a basic web laborer that include numbers behind the scenes:

HTML Web Workers
HTML Web Workers

Check Web Worker Support

Prior to making a web specialist, check whether the client’s program upholds it:

if (typeof(Worker) !== “undefined”) {
  // Yes! Web worker support!
  // Some code…..
} else {
  // Sorry! No Web Worker support..

Create a Web Worker File

Presently, we should make our web specialist in an outside JavaScript.

Here, we make a content that matters. The content is put away in the “demo_workers.js” record:

function timedCount() {
  i = i + 1;


The huge piece of the code above is the postMessage() strategy – which is used to introduce a message back on the HTML page.

Note: Normally web workers are not used for such simple scripts, but for more CPU intensive tasks.

Create a Web Worker Object

Since we have the web specialist record, we need to call it from a HTML page.

The accompanying lines checks if the laborer as of now exists, if not – it makes another web specialist article and runs the code in “demo_workers.js”:

if (typeof(w) == “undefined”) {
  w = new Worker(“demo_workers.js”);

Then, at that point we can send and get messages from the web specialist.

Add an “onmessage” occasion audience to the web specialist.

w.onmessage = function(event){
  document.getElementById(“result”).innerHTML = event.data;

At the point when the web specialist posts a message, the code inside the occasion audience is executed. The information from the web specialist is put away in event.data.

Terminate a Web Worker

At the point when a web specialist object is made, it will keep on tuning in for messages (even get-togethers outside script is done) until it is ended.

To end a web specialist, and free program/PC assets, utilize the end() strategy:

Reuse the Web Worker

On the off chance that you set the laborer variable to vague, after it has been ended, you can reuse the code:= undefined;

Full Web Worker Example Code

We have effectively seen the Worker code in the .js document. The following is the code for the HTML page:


<!DOCTYPE html>

<p>Count numbers: <output id=”result”></output></p>
<button onclick=”startWorker()”>Start Worker</button>
<button onclick=”stopWorker()”>Stop Worker</button>

var w;

function startWorker() {
  if (typeof(Worker) !== “undefined”) {
    if (typeof(w) == “undefined”) {
      w = new Worker(“demo_workers.js”);
    w.onmessage = function(event) {
      document.getElementById(“result”).innerHTML = event.data;
  } else {
    document.getElementById(“result”).innerHTML = “Sorry! No Web Worker support.”;

function stopWorker() {
  w = undefined;


Web Workers and the DOM

Since web laborers are in outer documents, they don’t approach the accompanying JavaScript objects:

  • The window object
  • The document object
  • The parent object