jquery Ajax Properties Async (Synchronous asynchronous) example _jquery

Source: Internet
Author: User
Example 1, jquery+ajax/"target=" _blank ">jquery Ajax sync Way
Copy Code code as follows:

$.ajax ({
URL: ' test.php ',
Type: ' Post ',
async:false,//How to use synchronization, true is asynchronous
Data: {' act ': ' Addvideo ', ' videoname ': videoname},//uses JSON objects here
Success:function (data) {
Code here ...
},
Fail:function () {
Code here ...
}
});

Example 2
Copy Code code as follows:

Javascript
function test ()
{
var a= 1;
$.ajax ({
Type: ' Get ',
URL: ' test.php ',
Data: ' page=112 ',
Success:function (msg)
{
Alert (msg);
A= msg;
}
})
alert (a);
}
test.php
Sleef (' 5 '); Take a five-minute break.
Echo ' in ';
/*
This program is run by printing 1 (a=1) and then five seconds after printing in
From this situation, you know the AJAX implementation process of jquery.
Because it is an asynchronous call
That's the way it used to be. Assigning a value to a variable is wrong no matter how it is done. Finally, we find this problem
Parameter async to False for synchronous calls when Ajax returns results, the program continues to execute
*/

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:
Example 3
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.
Attention
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 state, 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
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.