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 worker
What 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. DOM
limit
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,worker
How do threads listen for messages on the main thread? How is the message sent?worker
How 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 downworker
thread
1) Main thread is closedworker
thread
worker.terminate()
2)worker
Thread closed
self.close()
4,worker
How do threads load other scripts?
importScript('scripts.js')
importScript('scripts1.js'.'scripts2.js')
Copy the code
5, the main thread andworker
The threadAPI
The main thread | worker thread |
---|---|
Worker.onerror : specifyerror A listener function for the event |
self.name :Worker The name of the |
Worker.onmessage : specifymessage A listener function for the event |
self.onmessage : specifymessage A listener function for the event |
Worker.onmessageerror : specifymessageerror A listener function for the event |
self.onmessageerror : specifymessageerror A listener function for the event |
Worker.postMessage() To:Worker Thread sending message |
self.close() Closed:Worker thread |
Worker.terminate() : Immediate terminationWorker thread |
self.postMessage() : To produce thisWorker Thread sending message |
self.importScripts() Loading:JS The script |
The resources
- MDN Web Workers