This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

JavaScript Web Workers API

Count numbers:

// start the web worker API by clicking the start button
onclick="startWorker()"

// stop the web worker API by clicking the stop button
onclick="stopWorker()"

// defines the functions
let w;

function startWorker() {
  if(typeof(w) == "undefined") {
    w = new Worker("include/js/demo_workers.js");
  }
  w.onmessage = function(event) {
    document.getElementById("result").innerHTML = event.data;
  };
}

function stopWorker() { 
  w.terminate();
  w = undefined;
}

// the web worker external file "demo_workers.js"
let i = 0;

function timedCount() {
  i ++;
  postMessage(i);
  setTimeout("timedCount()",500);
}

timedCount();