admin管理员组

文章数量:1291616

HTML5 WebWorkers look very promising but at the moment they're not supported by IE8. I'm building a business Saas and I have to support this browser for at least another 2 years.

What would happen if I started implementing a web worker thread and ran it on IE8? Would it just run on a single thread or would it just not work at all?

Thanks.

HTML5 WebWorkers look very promising but at the moment they're not supported by IE8. I'm building a business Saas and I have to support this browser for at least another 2 years.

What would happen if I started implementing a web worker thread and ran it on IE8? Would it just run on a single thread or would it just not work at all?

Thanks.

Share Improve this question asked May 8, 2012 at 18:22 frenchiefrenchie 52k117 gold badges319 silver badges526 bronze badges 3
  • Did you try it? What happened? – epascarello Commented May 8, 2012 at 18:26
  • Not tried, it's not at that stage; just looking to see what would happen if I did. Have you tried it? – frenchie Commented May 8, 2012 at 18:31
  • 1 Takes 40 seconds to make a demo! Or test one of the demos online. :) – epascarello Commented May 8, 2012 at 18:46
Add a ment  | 

2 Answers 2

Reset to default 7

You'll get an error, because the API for creating Web Workers simply does not exist in IE.

e.g.

var worker = new Worker('my_task.js'); 

Will throw an error because Worker is undefined.

If you want to do feature detection, you can check first before creating the worker:

if(window.Worker !== undefined){
   var worker = new Worker('my_task.js'); 
}

Of course, whatever task you've delegated to the webworker won't happen on non-supported browsers, which means you'll need to run the logic on the primary (non worker) context.

Since you post message to (and listen for events/messages from) WebWorkers, if you follow the same approach to your worker task you can just run it in your primary context, and make calls to it in the same way as you would if it were a webworker. This will require some additional legwork on your end, but this way you can easily switch to WebWorkers when you detect it is supported in browser.

There is a project providing a dummy implementation of web workers for IE < 10: http://code.google./p/ie-web-worker/ The API is the same as, but the execution is single-threaded.

It works fine but I found one issue regarding this lib. The code of the worker is executed as soon as

var worker = new Worker('myworker.js');

is called. At this moment no

worker.onmessage = function {...}

is set and messages can't be sent from the worker to the main code. So it might be necessary to start your worker code not before a message is sent from the main code to the worker, e.g.

worker.postMessage('start');

本文标签: htmljavascript multithreading WebWorkers on IE8Stack Overflow