Web Workers

We use JavaScript for all of our client-side coding, but if a task takes a long time, we force the user to wait until the task has finished. Sometimes this even causes the user interface to become unresponsive. Web workers solve this problem by creating a simple way to write concurrent programs, or at least offload long-running tasks to the background.

Web workers are not part of the HTML5 specification, but if you need to do some background processing on the client side you should give web workers a closer look.[89]

Let’s look at a trivial example. We’ll use web workers to fetch data from YouTube’s public application programming interface (API), displaying a thumbnail that, when clicked, plays a video from YouTube. When it’s done, ...

Get HTML5 and CSS3, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.