jquery's Ajax synchronous and asynchronous understanding and examples

Source: Internet
Author: User

all the time I've been writing jquery code, the AJAX load data has to take into account the code's order of operations. Recent projects have been used to synchronize with Ajax. This synchronization means that when the JS code is loaded into the current AJAX will be the page all the code to stop loading, the page out of animation state, when this Ajax execution will continue to run other code page suspended animation state lifted.  
asynchronous is the same as when the Ajax code is running, as other code can run.  
The async:false of jquery, this attribute
The default is true: Asynchronous, false: synchronous.  

"POST""path", cache:false, async:false? "Text": "xml"function

with this property can relatively reduce the code to run the book Order problem, but if used too much, the page is too many suspended animation. This results in a poor user experience!  

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

Async
Boolean
default:true

by default, all requests was sent asynchronous (e.g. this was set to True by default). IF you need synchronous requests, set this option to false. Note that synchronous requests could temporarily lock the browser, disabling any actions and the request is Active. 

Success
Function

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

in this case, async defaults to a value of true, which is asynchronous, that is, when Ajax sends a request, in the process of waiting for the server to return, the foreground will continue to execute the script behind the Ajax block. The success is not executed until the server side returns the correct result, that is, two threads are executed, the AJAX block makes the request after a thread and the script behind the AJAX block (another thread) Example:

$.ajax ({type:"POST", url:"Venue.aspx?act=init", DataType:"html", success:  function//F1 (); F2 ();} failure:function  (Result) {alert (' Failed '

In the example above, when the Ajax block makes a request, he will stay function1 (), waiting for the server side to return, but at the same time (during this wait), the foreground will go to execute function2 (), that is, at this time two threads appear, For the moment we say Function1 () and function2 ().  

when the Asyn is set to false, when the AJAX request synchronous, that is, this time the AJAX block request, he will wait in the function1 () this place, will not go to execute function2 (), Know Function1 () part of the execution is complete.

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.