Ajax synchronous and asynchronous for jquery

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. jquery's Async:false, this property defaults to true: asynchronous, false: synchronous.
The form is as follows:
$.ajax ({
Type: "Post",
URL: "Path",
Cache:false,

Async:false,

        DataType: ($.browser.msie)? "Text": "xml",  
         success:function (xmlobj) { 
      & nbsp } 
}); The

has this property to reduce the number of code runs relative to the problem, but if used too much, the page is too many to feign death. This results in a poor user experience!
$. An official explanation of async and success in Ajax ():
async 
boolean 
Default:true
by Default, all requests is sent Asynchronous (e.g. this is 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.
The interpretation of this sentence: By default, all requests are asynchronous (this is because the default setting is True). If you need to synchronize the request, then set this option to false. It should be noted that the synchronization request may temporarily lock the browser, when the request is still executing, the other actions can not be executed.
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.

The explanation for this is that the method will be called when the execution of the request succeeds. This method gets two passed arguments: The data returned from the server side is the same as the datatype; Returns a character describing the state, which represents an Ajax time.

In this case, the async default setting value is True, which is asynchronous, that is, when Ajax sends a request, in the process of waiting for the server side to return, the foreground will continue executing the script behind the AJAX block until the server side returns the correct result to execute success , which means that this time the execution is two threads, the AJAX block makes the request after a thread and the AJAX block behind the script (another thread) example:
$.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 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.

(Source: http://www.cnblogs.com/xmphoenix/archive/2011/11/21/2257651.html)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ajax synchronous and asynchronous for jquery

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.