Jqueryajax attribute async (synchronous asynchronous) Example _ jquery-js tutorial

Source: Internet
Author: User
In jquery ajax, if we want to implement synchronization or Asynchronization, we can directly set async to true or false to truefalse, example 1: jquery + ajax/"target =" _ blank "> jquery ajax Synchronization Method

The Code is as follows:


$. Ajax ({
Url: 'test. php ',
Type: 'post ',
Async: false, // synchronous mode. true indicates asynchronous mode.
Data: {'ac': 'addvideo', 'videoname': videoname}, // json object is used here
Success: function (data ){
// Code here...
},
Fail: function (){
// Code here...
}
});


Example 2

The Code is as follows:


// Javascript
Function test ()
{
Var a = 1;
$. Ajax ({
Type: 'get ',
Url: 'test. php ',
Data: 'page = 100 ',
Success: function (msg)
{
Alert (msg );
A = msg;
}
})
Alert ();
}
// Test. php
Sleef ('5'); // five minutes off
Echo 'in ';
/*
This program runs in the following situation: Print 1 (a = 1) and then print in after five seconds
According to this situation, we can know the ajax execution process of jquery.
Because it is an asynchronous call
In the past, it was wrong to assign values to a variable. Finally, we found this problem.
If the async parameter is set to false, the program continues to execute the synchronous call after ajax returns the result.
*/


Here, the default value of async is true, which is asynchronous, that is, when ajax sends a request, it is waiting for the server to return this process, the foreground will continue to execute the script after the ajax block until the server returns the correct result to execute success. That is to say, two threads are executed at this time, example of the following code:
Example 3

The Code is 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 above example, when the ajax block sends a request, it will stop at function1 (), waiting for the server to return, but at the same time (in this waiting process ), the front-end will execute function2 (). That is to say, there are two threads at this time. Here we are talking about function1 () and function2 ().
When asyn is set to false, ajax requests are synchronized. That is to say, after the ajax block sends a request, it will wait at function1, function2 () is not executed, and function1 () is completed.
Note:
Synchronization means that when the JS Code is loaded to the current AJAX, all the code on the page will be stopped and the page will go out in a suspended state, after AJAX is executed, other code pages will continue to be run and the suspended state will be removed.
Asynchronous mode can run the same as other codes when the AJAX code is running.
Jquery's async: false, this attribute

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.