Ajax sets async to verify whether the user name exists. ajaxasync

Source: Internet
Author: User

Ajax sets async to verify whether the user name exists. ajaxasync

When you add a new user, you need to determine whether the mobile phone number exists. The initial idea is to set an onmouseout event on the textbox, which is very good in IE, but it is not very good at Google.

OK. Let's change your mind and check it when submitting the form:

// Check whether the mobile phone number has function checkRepeat () {var id = '$ {item. id} '; var mobile = $ ("# mobile "). val (); // alert (id); if (id = null | id = '') {$. ajax ({url: '/admin/adminuser/ajaxCheckReapet.shtml? Mobile = '+ mobile, type: 'get', ype: 'text', cache: false, async: false, timeout: 5000, error: function () {alert ('data acquisition failed! ');}, Success: function (msg) {if ("1" = msg) {$ ("# spMobile "). attr ("style", "display: block; color: red;"); $ ("# hiddenMobile "). attr ("value", "true");} else {$ ("# spMobile "). attr ("style", "display: none;"); $ ("# hiddenMobile "). attr ("value", "false") ;}}) ;}return true ;}function save () {if (checkSImg () & checkRepeat ()) {var hiddenMobile = $ ("# hiddenMobile "). val (); // alert (hiddenMobile); if (hiddenMobile = 'false') {if ($ ("# form1 "). form ("validate") {$ ("# form1 "). submit ();}}}}

In this process, I found a very interesting thing: When hiddenMobile returns false, the form is still submitted.

Ah, tangle, but also brainstorm, FK

I thought about it, thought about it, looked for it, and suddenly thought of async. Although I have never used it before, I just went off and added async: false. I wiped it, during the demo again, it actually worked.

Cache: false,
Async: false,

Well, to solve the problem, let's take a deeper look at it: Well, you have some gains. Let's take a look.

The default value of async is true, which is asynchronous. That is to say, when ajax sends a request, the foreground will continue to execute the script after the ajax block while waiting for the server to return, success will not be executed until the server returns the correct results. That is to say, two threads are executed at this time. After the ajax block sends a request, one thread and the script after the ajax block (another thread)

$.ajax({      type:"POST",      url:"Venue.aspx?act=init",       dataType:"html",      success:function(result){  //function1()       f1();        f2();     }      failure:function (result) {       alert('Failed');      },  }  function2(); 

In the above example, when the ajax block sends a request, it will stop at function1 (), waiting for the server to return, but at the same time (in this waiting process ), the front-end will execute function2 (). That is to say, there are two threads at this time. Here we are talking about function1 () and function2 ().

When asyn is set to false, ajax requests are synchronized. That is to say, after the ajax block sends a request, it will wait at function1, function2 () is not executed, and function1 () is completed.

Note:

Synchronization means that when the JS Code is loaded to the current AJAX, all the code on the page will be stopped and the page will go out in a suspended state, after AJAX is executed, other code pages will continue to be run and the suspended state will be removed.

Asynchronous mode can run the same as other codes when the AJAX code is running.

Jquery's async: false, this attribute

The above implementation method for setting async to verify whether the user name exists is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's house.

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.