Home  >  Article  >  Web Front-end  >  JavaScript thread running in the background--Web Worker

JavaScript thread running in the background--Web Worker

零下一度
零下一度Original
2017-06-25 09:28:341414browse

To put it simply, Web Worker is a JavaScript thread running in the background and will not affect the response of the page.

We know that JavaScript is a single-threaded scripting language, that is, it can only do one thing at the same time, otherwise it will cause extremely complex synchronization problems. For example, JavaScript has two threads at the same time. One thread is responsible for adding content to a certain DOM node, and the other thread deletes the node. At this time, which thread should the browser use as the main thread?

So, in order to avoid the problem of synchronization complexity, JavaScript has been single-threaded since its birth, which is also a characteristic of this language.

The single-thread mechanism of JavaScript will cause a problem. When there are some very complex tasks that need to be processed, the page has to wait for the task processing to be completed before it can respond to the user's operation. This has a negative impact on the page's response and user experience. will bring some negative effects. In order to solve this problem and to take advantage of the computing power of multi-core CPUs, HTML5 proposed the Web Worker standard, which allows JavaScript to create multiple threads, but these newly created threads will be used as child threads and are fully subject to Main thread control. And it is not allowed to operate the DOM. In fact, it is still single-threaded in nature. Therefore, we can hand over some time-consuming tasks to the sub-thread created by the Web Worker to complete in the background, while the foreground page can still process the user's response.

Since the thread created by Web Worker is a restricted sub-thread, there will be some usage restrictions:

  • Web Worker cannot access DOM nodes;

  • Web Worker cannot access global variables or global functions;

  • Web Worker cannot call functions such as alert() or confirm;

  • Web Worker cannot access browser global variables such as window and document;

However, Javascript in Web Worker can still use setTimeout() and setInterval(). Class functions can also use XMLHttpRequest objects for Ajax communication.

Currently all major browsers support web workers except Internet Explorer.

Friends who are familiar with Angular should know that Angular1 is most criticized for its dirty checking mechanism. When the scope has too much data, it will seriously affect performance. Angular2 uses WebWorker to move heavy calculation work into the auxiliary thread so that the interface thread is not affected.

The above is the detailed content of JavaScript thread running in the background--Web Worker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn