Javascript multithreading?

Source: Internet
Author: User

Recently encountered a problem: the page in the HTML page cannot be refreshed, only after the bottom of all processing is completed before the page refresh. It has been treated with settimeout, but it has not achieved the desired effect.

The main reason is that the function of settimeout is not clear and there is no clear understanding of JavaScript single thread.

Introduction to threading in the browser

Usually a browser will have at least three threads: JS engine thread (for processing JS), GUI render thread (for page rendering), Browser time trigger thread (for controlling interaction).

Because JS can manipulate DOM elements, which in turn affects the rendering of the GUI, the JS engine thread is mutually exclusive to the GUI rendering thread . This means that when the JS engine thread is in the running state, the GUI render thread will be in a frozen state.

The JS engine is event-driven and uses a single-threaded operation mechanism. That is, the JS engine will only take the task from the task list sequentially and execute it.

Settimeout/setinternal

Where Settiemout: The specified code snippet is called after the specified number of milliseconds, and setinternal: The specified code snippet is called within the specified time interval (ms) loop. Both functions involve time counters, which are all related to a similar with the MFC timer. The JS engine itself can only be run single-threaded, so the timer needs to be started by other external threads. So for the JS engine, the timer thread can be considered an asynchronous thread. However, when the timer time arrives, the triggered event must be queued in the task list, waiting for the JS engine to be processed.
About SetTimeout Here's an example that can help in-depth understanding:

SetTimeout (function () {while (true) {}}, 1000);
SetTimeout (function () {alert (' End 2 ');}, 2000);
SetTimeout (function () {alert (' End 1 ');}, 100); Alert (' End ');

The result of the execution is the popup ' End ' End 1 ', then the browser is suspended animation, that is, do not eject ' end 2 '. In other words, the first settimeout is executed in a dead loop, which leads directly to the theory that the function in the second settimeout that is executed later in the second is blocked, which is inconsistent with the asynchronous multithreading that we normally understand.

Pages referenced by:

Example:

Http://www.cnblogs.com/jeffwongishandsome/archive/2011/06/13/2080145.html

Introduction to the function of settimeout/setinternal and examples of its use

Http://www.w3school.com.cn/htmldom/met_win_settimeout.asp

Http://www.w3school.com.cn/htmldom/met_win_setinterval.asp

JS Multi-threaded introduction (inside drew a description of the task queue mechanism schematic diagram, the picture is clear, the explanation is also very clear)

http://blog.csdn.net/nx8823520/article/details/7513098

Javascript multithreading?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.