Introduction
Support Detection
Parallel Job
Manage the flow
Error handling
Sample
When I tried to run the sample, I entered a string in the userName "slow". Clicked on the slow button which led the delay loop to execute. I could, in the meantime, interact with the application, update the string in the input text box and click the second button. The second button also spawned a worker which returned almost instantly, displaying the updated input and the time.
- /* myWorker.js */
- //only for demo purpose
- function ReallySlowCode(millis)
- {
- var date = new Date();
- var curDate =
- null;
- do {
- curDate = new Date();
- }
- while (curDate - date < millis);
- }
- function slowfunc(userName) {
- ReallySlowCode(5000);
- return userName +
- "-" + new Date();
- }
- function quickfunc(userName) {
- return
- userName + "-" + new Date();
- }
- /* The onmessage event listener
- responds to the event raised from the main script, invokes the appropriate
- function based on the data passed.
- */
- this.onmessage = function(event) {
- var data = event.data;
- switch (data.op) {
- case 'slow':
- postMessage(slowfunc(data.userName));
- break;
- case 'quick':
- postMessage(quickfunc(data.userName));
- break;
- default:
- postMessage("Wrong
- operation specified ");
- }
- };
Main: named as testww.html
- <!DOCTYPE html>
- <body>
- <table border="0">
- <tr>
- <td>Your
- Name</td>
- <td>
- <input type="text" id="userName"
- />
- </td>
- </tr>
- <tr>
- <td>Slow
- Process</td>
- <td>
- <input type="text" id="slowResult"
- size="100"/>
- </td>
- </tr>
- <tr>
- <td>Quick
- Process</td>
- <td>
- <input type="text" id="quickResult"
- size="100"/>
- </td>
- </tr>
- <tr>
- <td
- colwidth="2">
- <input type="button" id="slowButton" value="Run Long
- process" />
- <input type="button" id="quickButton" value="Run quick
- process" />
- </td>
- </tr>
- </table>
- <script>
- /* Check if Web Workers are supported
- */
- function getWebWorkerSupport() {
- return (typeof(Worker) !==
- "undefined") ? true:false;
- }
- if(getWebWorkerSupport()
- == true)
- {
- var userName,y,message;
- /* Create the new workers.
- each instance runs in parallel */
- slowWorker = new
- Worker("myWorker.js");
- quickWorker = new
- Worker("myWorker.js");
- /* Bind an event listener for the onmessage
- function for each of the workers. this will be invoked by the worker thread when
- the execution has completed.
- */
- slowWorker.onmessage = function (event)
- {
- document.getElementById("slowResult").value =
- event.data;
- };
- quickWorker.onmessage = function (event)
- {
- document.getElementById("quickResult").value = event.data;
- };
- /* Register events for buttons
- */
- document.getElementById("slowButton").onclick = function() {
- userName = document.getElementById("userName").value;
- message = {
- 'op'
- : 'slow',
- 'userName' :
- userName
- };
- slowWorker.postMessage(message);
- }
- document.getElementById("quickButton").onclick
- = function() {
- userName = document.getElementById("userName").value;
- message = {
- 'op'
- : 'quick',
- 'userName' :
- userName
- };
- quickWorker.postMessage(message);
- }
- }
- </script>
- </body>undefined</html>
Dipal ChoksiPosted Jul 24, 2011, 7:38 PM
Isn't it such an amazing feature? I haven't yet used web workers in PROD apps. There is a good deal of information on the concepts but not much info on benchmarks. Here's an article with some advanced real-like samples: "10 web workers": http://www.whatwg.org/specs/web-apps/current-work/complete/workers.html
Mahesh ChandPosted Jul 21, 2011, 11:04 PM
Thanks Dipal. I didn't know HTML can can multiple threads. Nice :) I can really use this feature in some of our new features like processing some background data. How's the performance? Wonder if you or anybody here have used in real applications?