HTML5 Web Workers Flashcards
7 cards from real HTML5 practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 HTML5 Web Workers flashcards as text
What is the primary purpose of HTML5 Web Workers?
Answer: To run JavaScript in a background thread without blocking the main UI thread
Web Workers allow scripts to run in background threads, keeping the main thread free and preventing UI freezing during heavy computations.
Which of the following correctly creates a new Web Worker?
Answer: var worker = new Worker('worker.js');
The Worker constructor takes the URL of a script file as its argument to instantiate a new background worker.
How does the main thread send a message to a Web Worker?
Answer: worker.postMessage(data)
The postMessage() method is used on both sides of the worker communication channel to pass data.
Inside a Web Worker, which global object is available instead of 'window'?
Answer: self
Inside a Web Worker, 'self' refers to the worker's global scope (DedicatedWorkerGlobalScope), since 'window' is not available.
Which of the following resources is NOT accessible inside a Web Worker?
Answer: The DOM (document object)
Web Workers run in a separate thread and do not have access to the DOM, window, or document objects for thread-safety reasons.
How do you terminate a Web Worker from the main thread?
Answer: worker.terminate()
The terminate() method is called on the worker instance from the main thread to immediately stop the worker.
Which event listener is used in the main thread to receive messages from a Web Worker?
Answer: worker.onmessage
The 'onmessage' event handler fires in the main thread whenever the worker calls postMessage(), with the data accessible via event.data.