Ajax synchronous and asynchronous understanding of jquery and example _ajax related

Source: Internet
Author: User
have been writing jquery code before the encounter AJAX load data need to consider the sequence of code running problems. The most recent projects have been synchronized with Ajax. This synchronization means that when the JS code is loaded into the current Ajax will put all the code in the page to stop loading, the page out of suspended animation, when this Ajax execution will continue to run the other code page suspended animation state.
Asynchronous, the Ajax code runs the same way that other code can.
The Async:false of jquery, this attribute
The default is true: Asynchronous, false: Sync.
Copy Code code as follows:

$.ajax ({

Type: "Post",

URL: "Path",

Cache:false,

Async:false,

DataType: ($.browser.msie)? "Text": "XML",

Success:function (xmlobj) {

}

});

With this attribute can be relatively reduced code to run the book Order problem, but if used too much, the page suspended animation too many times. This leads to poor user experience ~!

$. An official explanation of async and success in Ajax ():

Async
Boolean
Default:true

By default, all requests are sent asynchronous (e.g. this is set to True by default). If you are need synchronous requests, set this option to false. Note This synchronous requests may temporarily lock the browser and disabling any actions while the ' request is active '.

Success
Function

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

In this case, the default set value of Async is true, which is asynchronous, that is, when Ajax sends a request, the foreground will continue to execute the script behind the AJAX block until the server side returns the correct result before it executes the success , which means that the two threads are executed at this time, and the AJAX block emits a script (another thread) after the request followed by a thread and an AJAX block:
Copy Code code as follows:

$.ajax ({

Type: "POST",

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

DataType: "HTML",

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

F1 ();

F2 ();

}

Failure:function (Result) {

Alert (' Failed ');

},

}

Function2 ();

In the example above, when the Ajax block makes a request, he stays function1 () and waits for the server to return, but at the same time (during the wait), the foreground executes function2 (), that is, there are two threads at this time, We are here to say Function1 () and function2 ().

When the Asyn is set to False, it is synchronized when the AJAX request is made, that is, when the Ajax block makes a request, he waits in the function1 () and does not perform function2 (), knowing that the function1 () part of the execution is complete.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.