Ajax Synchronous and asynchronous

Source: Internet
Author: User

Let's talk about synchronization and Asynchrony in Ajax today. In fact, as I understand it, the difference between synchronous and asynchronous is whether there is waiting in the process of program execution.

Synchronization: This means that the JS code is loaded into the current Ajax, waiting for the Ajax code to execute before starting to load other code;

Async: JS code is when loaded into Ajax, while the Ajax code executes, but also executes other code.

The async attribute of jquery: True by default: Async, labeled false: Indicates synchronization

Official explanation:

Async
Boolean
Default: True

By default, all requests be sent asynchronous (e.g. this is set to True by default). IF you need synchronous requests, set this option to false. Note that synchronous requests could temporarily lock the browser, disabling any actions and the request is active.

The translation is as follows: Asynchronous processing: The return type is Boolean,

By default, the request is handled asynchronously, the return value is true, and if a requirement for synchronization is required, the return value can be set to false, but note that the browser may be temporarily locked when synchronizing the request, and any other behavior is prohibited while the synchronous request behavior is active.

Success
Function

A function to is called if the request succeeds. The function gets passed arguments:the data returned from the server, formatted according to the ' DataType ' parameter , and a string describing the status. This is an Ajax Event.

The translation is as follows: When the function requests a successful return, it will get 2 parameters, 1. The data returned from the server side. 2. This is an AJAX event, depending on the format of the data type parameter and the string describing the state.

Async: When Ajax sends a request, while waiting for the server side to return, the foreground will continue to execute the code behind AJAX until the server side returns the correct result before executing the success, which is no wait process and is divided into 2 threads when executing.

Look at the following code:

$.ajax ({

Type: "POST",

URL: "Venue.aspx?act=init",

DataType: "HTML",

Success:function (Result) {//function1 ()

F1 ();

F2 ();

}

Failure:function (Result) {

Alert (' Failed ');

}

}

Function2 ();

As we have already said, by default, async defaults to True, asynchronous processing, that is, when the Ajax sends the request, will stay in the function1 (), waiting for the server to return results, but during the wait process, the foreground will continue to execute function2 (), this time there is a 2 threads.

Synchronous processing: When Async is set to False, after Ajax makes a request, it waits for function1 () and does not execute function2 () until part of Function1 () is executed.

Ajax Synchronous and asynchronous

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.