$. Ajax async usage in jquery

Source: Internet
Author: User

Async is true by default, that is, asynchronous mode, $. after Ajax is executed, the following steps are continued after ajax is executed until the server returns data, triggering $. success method in Ajax. at this time, two threads are executed.

 

The code is as follows: Copy code

Var temp;
$. Ajax ({
Async: false,
Type: "POST ",
Url: defaultPostData. url,
DataType: 'json ',
Success: function (data ){
Temp = data;
}
});
Alert (temp );

This ajax request is a synchronous request. alert (temp) will not execute until no return value is returned.

If async is set to true, the ajax statement is executed directly without waiting for the results returned by the ajax request.

Note:

Asynchronization is equivalent to starting ajax requests in the form of a thread.

Synchronization is equivalent to executing ajax requests step by step. Only after the ajax request is executed can the subsequent statements be executed.

Example

The code is as follows: Copy code

<Form action = "1.jsp">
<Input type = "text" id = "tel" flag = "0"/>
<Input type = "text" id = www.111cn.net flag = "0"/>
<Input type = "submit" value = "submit"/>
</Form>
<Script>
$ ('# Tel, # email'). blur (function (){
Var _ this = this;
$. Ajax ({
Type: 'post ',
Url :'',
// Async: false,
DataType: 'text ',
// Data :'',
Success: function (data ){
If (data = true ){
$ (_ This). attr ('flag', 1)
                }       
            }
});
});
   
$ ('Form'). submit (function (){
Var isSubmit = true;
Var input = $ ('input: text ');
For (var I = 0; I <input. length; I ++ ){
Input. eq (I). blur (); // Click submit for verification again
If (input. eq (I). attr ('flag') = 0 ){
IsSubmit = false;
            }   
        }
Alert (isSubmit );
Return isSubmit;
});
</Script>

Click "submit". All input boxes undergo an ajax verification. If both return true, the form is submitted;
If it is not commented out: async: false, it is okay, but after it is commented out, because it is asynchronous transmission, the flag value of the submit has not been returned yet, so all are false
The form cannot be submitted.
If the default async: true is selected, how can this problem be determined, and then submit


The async parameter of ajax causes the Firefox browser to flash the screen

My pop-up screen is:

$. Ajax ({
Type: "post ",
Url: "index. php ",
Data :{},
Async: false,
BeforeSend: function (){},
Success: function (data ){
//...
},
Complete: function (){}
});

Here, my async is set to false. The original intention is to return data and then execute $. the script behind Ajax did not expect it to cause a pop-up screen in Firefox. (Firefox 11.0), scroll down to the bottom to trigger ajax. pop-up screen

Finally, comment out async: false, that is, if async is true, the problem of triggering a flash screen in ajax is solved successfully when the scroll bar of Firefox browser is pulled down to the bottom.

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.