Category Archives: HTML5 Web Workers

HTML5 Web Workers Simple pool of workers

Within this example, the browser would develop a pool of workers and delivering the command to every workerand begin the task. not so difficult, you may still find many complication coming, stay tune.

var workers = new Array(5);
// creating a pool of workers
for(var i=0; i<5; ++i) {
  workers[i] = new Worker('worker001.js');
  // getting the message from the worker
  workers[i].onmessage = function(event) {
for(var i=0; i<5; ++i) {
  // sending the message to the worker

and here is code of the worker001.js

var ID = null;

onmessage = function(event) {
  ID =;
  postMessage('worker: ID ('+ID+') assigned and start to run');

nevertheless, when you look into the implementation of the chrome, you will find it is a separate process!

Incoming search terms:

  • tutorial webworker html5
  • web workers html5 example

Working with HTML5 Web Workers to have background computational capability

When carrying out superior load-heavy functions in a web browser, both the web page it is run in as well as the web browser User interface turns into unresponsive till it’s completed. But, there’s a method to handle that with HTML5 Web Workers.

What HTML5 Web Workers do

Fundamentally, Web Workers provides you a chance to load a JavaScript file dynamically and then have it process scri[t in a background process, not affecting the user interface and its response level. You can continue to do whatever you want, selecting thing, clicking etc, while all Web Worker computation influences background.

Working with HTML5 Web Workers

First, you create a Worker object. Then you can certainly send text data to it with a postMessage method, it can send information back with its own postMessage and both of them have onmessage event handlers to receive data. For instance, here there is a simple example where you send in some data to a Web Worker, have it do 1 000 000 iterations in a loop, and continually post the current value back:

In the web page

// Creates a Web Worker
var worker = new Worker("worker.js");

// Posts a message to the Web Worker

// Triggered by postMessage in the Web Worker
worker.onmessage = function (evt) {
	// is the values from the Web Worker

// If the Web Worker throws an error
worker.onerror = function (evt) {

In the Web Worker JavaScript file

// Triggered by postMessage in the page
onmessage = function (evt) {
	// will be 0 here from the above postMessage
	for (var, il=1000001; i<il; i++) {
		// Continually sends data back

In addition to the postMessage method and the onmessage event used above, there is a handy terminate method on the Worker object, to instantly stop all of its work.

This demo and complete code comes in the HTML5 Web Worker Demo page, which is part of my HTML5 – Information and samples for HTML5 and related APIs playground.

Web Browser support

So far, Web Workers are supported in Firefox 3.5 and Safari 4, and soon upcoming in Google Chrome.


This is quite nice for heavy operations. However, there are to main issues related to the terminate method:

Firefox and execution of terminate

As you have seen in the HTML5 Web Worker Demo, in Firefox, if you click on the “Stop Worker” button, it isn’t actually called till the previous ongoing operation is finished. To me, this practically renders it useless since the whole idea of the terminate method should really be instant, and which appears to be the idea according to the Mozilla Web Worker documentation for terminate():

So that you can immediately terminate a running worker, you can do so by calling the worker’s terminate() method… The worker thread is killed immediately without a chance to complete its operations or clean up after itself.

My take is that this is a bug, and I will talk to Mozilla about this.

Can’t restart a terminated Web Worker

In both Firefox and Safari, if you have called the terminate method on a Web Worker, there’s no way to get it started again by calling postMessage on it. Instead, you should reinstantiate the Worker with the new Worker(“filename.js”) again, which sort of defeats the idea of pausing/playing a Worker’s action. I don’t know if this describes due to design or implementation, but if you craft your code cleverly, you can work around this.

Spreading the load

Despite the issues I think Web Workers are great, and they could prove fantastic with regards to relieving the user interface from being frozen when having to do complex calculations and similar.

Incoming search terms:

  • html5 web workers example
  • html5 web worker example
  • web worker html5 example
8 visitors online now
3 guests, 5 bots, 0 members
Max visitors today: 16 at 01:56 am UTC
This month: 74 at 04-21-2017 07:18 pm UTC
This year: 74 at 04-21-2017 07:18 pm UTC
All time: 513 at 06-01-2016 02:24 pm UTC
Get Adobe Flash player