Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create background threads in JavaScript

This question has been asked and answered here and here. However, both threads are 18+ months old and things are evolving rapidly in the javascript world. So I'm asking again to get an up to date answer.

For our use we need a single background thread that will be running pretty regularly. It's for a rich editor and whenever the user edits, this background thread needs to recalculate the layout of the rich text.

It looks like Web Workers are the best approach. However, according to wikipedia "It is removed in newer Android browser versions".

Questions:

  1. Are Web Workers the best approach or is there something better.
  2. Are there browsers that are not going to support Web Workers going forward? And if so, which ones?
  3. Is there a list of what version of the main browsers are required for Web Workers?
like image 849
David Thielen Avatar asked Feb 08 '26 02:02

David Thielen


1 Answers

I would recommend WebWorkers as your first option.

True, they were removed from Android in 2.2, but were also re-added in 4.4 and are now supported by the latest version of all major platforms. See http://caniuse.com/webworkers to see in which version WebWorkers are available.

For browsers without support for WebWorkers I would recommend a fallback with postMessage() or setTimeout(), as detailed in Javascript - how to avoid blocking the browser while doing heavy work?

like image 169
Aron Cederholm Avatar answered Feb 09 '26 14:02

Aron Cederholm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!