Supported by most modern browsers (with the exception of Internet Explorer, although rumors are that the final release of its version 9 will let you use them in a pretty standard fashion), web workers can be easily reached from the main web page through a simple messaging API, whose main elements are the “onmessage” event handler and the “postMessage()” method. The use of this tandem makes it really simple to dispatch messages to one or multiple workers - and in turn - receive responses from them for further manipulation.
In earlier chapters of this series I developed a couple of examples which demonstrated how to interact with a single worker from a main script, and how to spawning multiple workers.
If you missed those parts - or would like to refresh your memory - you can find them here:
While the examples above were hopefully helpful, you may be wondering if it’s possible to stop the web workers from the main script. Well, the answer is an astonishing yes! Effectively, the API includes a method called “close()”, which permits you to close the thread associated to a specific worker.
In the lines to come I’ll be setting up another basic example, which will illustrate how to use the “close()” method to stop a single web worker from a main web page.