Horje
Full Web Worker Example Code

We have already seen the Worker code in the .js file. Below is the code for the HTML page:


Full Example Web Worker

Follow the Example
index.html
Example: HTML
 <!DOCTYPE html>
<html>
<body>

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

<script>
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.terminate();
  w = undefined;
}
</script>

</body>
</html> 

Output should be:

Full Example Web Worker





Category:
Web Tutorial
Sub Category:
HTML Web Workers API
Uploaded by:
Admin


Read Article
https://develop.horje.com/learn/1434/reference