jquery Ajax synchronous and asynchronous attributes async detailed

Source: Internet
Author: User

Example 1. jquery+ajax/"target=" _blank ">jquery Ajax sync Way

The code is as follows Copy Code

$.ajax ({

URL: ' test.php ',

Type: ' Post ',

Async:false,//Using synchronous method, True is asynchronous

Data: {' act ': ' Addvideo ', ' videoname ': videoname},//uses JSON objects here

Success:function (data) {

Code here ...

},

Fail:function () {

Code here ...

}

});


Example 2

  code is as follows copy code

//javascript
Function test ()
{
 var a= 1;
 $.ajax ({ br>   type  : ' Get ',
  url   : ' test.php ',
  data   : ' page=112 ',
  success:function (msg)
  {
   alert (msg) ;
   a= msg;
  }
 })
 alert (a);
}

//test.php
Sleef (' 5 ');//Rest five minutes
echo ' in ';

/*
  This program is running   first print 1 (a=1) then five seconds later print in
  According to this situation can know the AJAX implementation process of jquery
  because it is asynchronous call
&nb SP; Assign a value to a variable   it's wrong to do it anyway. Finally found this problem
  parameter async to False for synchronous calls when Ajax returns results after the program continues
*/

Example 3

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:

The code is as follows Copy Code

$.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, when this Ajax execution will continue to run the other code page suspended animation state.
and asynchronous, the Ajax code runs as well as other code.
The Async:false of jquery, this property

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.