Home > Web Front-end > JS Tutorial > jquery ajax attribute async (synchronous and asynchronous) example_jquery

jquery ajax attribute async (synchronous and asynchronous) example_jquery

WBOY
Release: 2016-05-16 17:17:23
Original
1041 people have browsed it

Example 1, jquery ajax/" target="_blank">jquery ajax synchronization method

Copy code The code is as follows:

$.ajax({
url : 'test.php',
type : 'post',
async: false,//Use synchronous method, true is asynchronous method
data: {'act':'addvideo', 'videoname':videoname},//Json object is used here
success: function(data){
//code here...
} ,
fail:function(){
//code here...
}
});

Example 2
Copy code The code is 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
sleep('5'); //rest for five minutes
echo 'in';
/*
The operation of this program is to print 1 (a=1) first and then print in
after five seconds. Based on this situation, you can know the execution process of jquery's ajax
Because it is an asynchronous call
This is how you assign a value to a variable. No matter how you do it, it is wrong. Finally, you find this problem
The parameter async is changed to false. It is a synchronous call. When ajax returns the result, the program will continue to execute
*/

Here, the default setting value of async is true. In this case, it is an asynchronous method, that is, when ajax sends After the request, while waiting for the server to return, the front desk will continue to execute the script behind the ajax block. Success will not be executed until the server returns the correct result. That is to say, two threads are executed at this time, the ajax block. One thread after making the request and a script behind the ajax block (another thread) Example:
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' ; , but at the same time (during this waiting process), the foreground will execute function2(), that is to say, two threads will appear at this time, let's call them function1() and function2() here.
When asyn is set to false, the ajax request is synchronous. That is to say, after the ajax block sends a request at this time, it will wait at function1() and will not execute function2(). Know that the function1() part is executed.
Note
Synchronization means that when the JS code is loaded into the current AJAX, all the code in the page will stop loading, and the page will go out of the suspended state. After the AJAX is completed, other codes will continue to run and the page will be suspended. Lift.
Asynchronously, other codes can run while this AJAX code is running.
jquery’s async:false, this attribute
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template