techani21
10/28/2017 - 8:33 PM

HTML5 Web Workers example

HTML5 Web Workers example

var i=0;

function timedCount()
{
i=i+1;
postMessage(i);                   //posts a message back to the HTML page.
setTimeout("timedCount()",500);
}

timedCount();
<!-- When executing scripts in an HTML page, the page becomes unresponsive until the script is finished. -->

<!-- A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting -->
<!-- the performance of the page. You can continue to do whatever you want: clicking, selecting things, etc., -->
<!-- while the web worker runs in the background. -->

<!DOCTYPE html>
<html>
<body>

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


<script>
var w;

function startWorker()
{
if(typeof(Worker)!=="undefined")            //check whether the user's browser supports it
  {
  w=new Worker("demo_workers.js");          //creates a new web worker object and runs the code in "demo_workers.js"

  //Add an "onmessage" event listener to the web worker
  //When the web worker posts a message, the code within the event listener is executed. The data from the web worker
  //is stored in event.data.
  w.onmessage = function (event) { document.getElementById("result").innerHTML=event.data; };
  }
else
  {
  document.getElementById("result").innerHTML="Sorry, your browser does not support Web Workers...";
  }
}

function stopWorker()
{ 
w.terminate();         //terminate a web worker, and free browser/computer resources
}
</script>

</body>
</html>