How can I use Web Workers in HTML5?


Web Workers allow for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions and allows long tasks to be executed without yielding to keep the page responsive.

Web Workers are background scripts and they are relatively heavyweight and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four-megapixel image.

Web Workers initialized with the URL of a JavaScript file, which contains the code the worker will execute. This code sets event listeners and communicates with the script that spawned it from the main page.

Syntax

Following is the syntax −

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

Updated on: 25-Jun-2020

110 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements