Ajax synchronous and asynchronous detailed _ajax related to jquery

Source: Internet
Author: User

have been writing jquery code before the encounter AJAX load data need to consider the sequence of code running problems. The most recent projects have been synchronized with Ajax. This 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.

Asynchronous, the Ajax code runs the same way that other code can.

The Async:false of jquery, this attribute

The default is true: Asynchronous, false: Sync.

$.ajax ({ 
    type: "Post", 
    URL: "Path", 
    Cache:false, 
    async:false, 
    dataType: ($.browser.msie)? "Text": "xml", 
     success:function (xmlobj) { 
    } 
});

With this attribute can be relatively reduced code to run the book Order problem, but if used too much, the page suspended animation too many times. This leads to poor user experience ~!

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

Async 
Boolean 
default:true
by Default, all requests are sent asynchronous (e.g. this is set to True by Defaul T). If You are need synchronous requests, set this option to false. Note This synchronous requests may temporarily lock the browser and disabling any actions while the ' request is active '.
Success 
function
A function to is called if the request succeeds. The function gets passed two arguments:the data returned from the server, formatted according to the ' DataType ' parameter , and a string describing the status. This is the Ajax Event.

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:

$.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.

The above content, is this article to introduce the jquery in the Ajax synchronization and the asynchronous detailed explanation, hoped everybody likes.

Related Article

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.