JS Fun Single Thread

Source: Internet
Author: User

a . JS's implementation characteristicsdue to the single-threaded features, JS can only execute part of the code over a period of time, then, when there are multiple pieces of code to execute, you need to wait in line. Two. Single thread vs. asynchronous events(1) What is an asynchronous event?An asynchronous event is an action such as a mouse click, a timer release, a XMLHttpRequest request, and since we do not know when it will be executed, it can be considered out of sync (these descriptions can only be used as a reference to help understand, O (∩_∩) o). (2) is the asynchronous event affected by a single thread?The answer is yes: Asynchronous events must also be queued (that is, the JS callback function that corresponds to the asynchronous event, which must also be queued for execution, and how the queue will vary depending on the browser). we often use settimeout to "improve bad waiting", and here is a settimeout example to show the unique charms of a single thread: //Code<script type= "Text/javascript" >

function Showcurrenttime () {
var date = new Date ();
var now = "";
now = Date.getfullyear () + "-";
now = Now + (Date.getmonth () +1) + "-";
now = Now + date.getdate () + "";
now = Now + date.gethours () + ":";
now = Now + date.getminutes () + ":";
now = Now + date.getseconds ();
return now;
}

function Test () {
Console.log (' a_time: ' + showcurrenttime ());

Access to the connection is delayed by 12 seconds
$.post (' Http://www.xxxxx.com/oilcard/index ', ', function () {
Console.log (' b_time: ' + showcurrenttime ());
}, ' html ');
Console.log (' c_time: ' + showcurrenttime ());
}

Test ();
SetInterval ("Console.log (' d_time: ' + showcurrenttime ())", 1000);

</script> //Execution Results

Execution successively: a,c,d,b;

 //Analysis of the results of the implementationfrom the result it can be obvious that the delay function is executed after the Add function is executed, why is this?timeout is only 15ms, and the Add function takes more time to execute than 15ms, that is, the Add function has not finished executing, the timer is released, but because of a single thread, the delay function is not executed immediately (because the Add function is still executing in the current time period), and at this point the browser queues the delay function so that it can be executed within the next executable time period. (Process as)Another example: remove the delay of 12 second response; results: The execution is: ACBD; you see, in the code block, code in the same scope takes precedence.
The same example is not reproduced in PHP, and is definitely prioritized by language.

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.