Web Worker

1. What isweb worker? What are the benefits? What are the problems?

Web workers create a multithreaded environment for JavaScript, allowing the main thread to create Worker threads and assign some tasks to the latter to run. While the main thread is running, the Worker thread is running in the background without interfering with each other. Wait until the Worker thread completes the calculation and returns the result to the main thread.

benefits:

The benefit is that when computationally intensive or high-latency tasks are taken on by Worker threads, the main thread (which is usually responsible for UI interactions) will flow smoothly and not be blocked or slowed down.

Question:

Once a Worker thread is created, it is always running and is not interrupted by activity on the main thread, such as a user clicking a button or submitting a form. This facilitates communication that responds to the main thread at any time. However, this also causes the Worker to consume resources, so it should not be overused and should be closed once it is used.

2, use,web workerWhat are the restrictions?

1. Homology restriction

The script file assigned to worker must be of the same origin as the main thread script file.

2. DOMlimit

The worker thread cannot read the DOM object of the page where the main thread is located. Instead of using document, window, parent objects, it can use navigator and Location objects.

3. Communication restrictions

The worker thread and the main thread are no longer in the same context and cannot communicate directly, which must be done through messages.

4. Script limitations

Worker threads cannot execute alert and confirm methods, but can make Ajax requests.

5. File restrictions

Worker threads cannot read local files, cannot open file systems, and must load scripts from the network that cannot be file:// files.

3,workerHow do threads listen for messages on the main thread? How is the message sent?workerHow is the thread closed?

Worker threads need to have a listener function inside that listens for message events.

/ / to monitor
self.addEventListener('message'.function (e) {
  // Send a message
  self.postMessage('You said: ' + e.data);
}, false);
Copy the code

Shut downworkerthread

1) Main thread is closedworkerthread

worker.terminate()

2)workerThread closed

self.close()

4,workerHow do threads load other scripts?

importScript('scripts.js')
importScript('scripts1.js'.'scripts2.js')
Copy the code

5, the main thread andworkerThe threadAPI

The main thread workerthread
Worker.onerror: specifyerrorA listener function for the event self.name:WorkerThe name of the
Worker.onmessage: specifymessageA listener function for the event self.onmessage: specifymessageA listener function for the event
Worker.onmessageerror: specifymessageerrorA listener function for the event self.onmessageerror: specifymessageerrorA listener function for the event
Worker.postMessage()To:WorkerThread sending message self.close()Closed:Workerthread
Worker.terminate(): Immediate terminationWorkerthread self.postMessage(): To produce thisWorkerThread sending message
self.importScripts()Loading:JSThe script
The resources
  • MDN Web Workers