JS in the Ajax execution order to solve the problem details (with the solution)

Source: Internet
Author: User

This article mainly explains the problem of the sequence of Ajax execution in JS, and now let's take a look at this article on Ajax execution order resolution.

In JS we will encounter the order of execution, especially the order of Ajax execution, JS in the default execution order is from the top down execution.

Look at the following section of code

    Callback:function (value, validator, $field) {$.ajax ({  url:window.ctx+ "/sys/manager/validateloginname",  Data:{loginname:value},  type: ' Post ',  dataType: ' json ',  async:true,    success:function (Result) {  if (result!=null)  Globalvariable.flag=result;  Alert (1)       }});  Alert (2)  if (globalvariable.flag!=1) return true;  if (globalvariable.flag==1) return false;  }

Because Ajax here is an asynchronous request, it pops up in the browser 2 in the popup 1

This will cause a problem if flag default is 0, after the completion of Ajax will become 1, then the IF statement is actually using 0 to do the judgment, and our purpose does not match, What we want is to use the flag that was assigned after Ajax to do the If judgment (want to see more on the Topic.alibabacloud.comAJAX Development Manual section of the study)

Solve:

The first of these methods

The problem is that Ajax uses asynchronous requests, so if we want to pop up 1 and then eject 2, we need to change Ajax to synchronous, that is, async to False

This way, if Ajax does not finish the page will appear suspended animation, stop execution, only when the Ajax callback will go down after the

Of course, we use Ajax just to be asynchronous, so the above approach is a special need to handle

The second method of

The second approach is more commonly used

For example, the following section of code

function test () {$.ajax ({  url:window.ctx+ "/sys/manager/adduserrole",  data:formdata,  type: ' Post ',  dataType: "JSON",          Processdata:false,          contenttype:false,  success:function (result) {  if ( Result!=null) {  testcallback ();}}  );         Test2 (); }function Testcallback () {alert (1)}function test2 () {alert (2)}

Ajax is asynchronous, we want to pop up 1 and then eject 2 we just need to put the test2 in the test callback function.

Like this

function test () {$.ajax ({  url:window.ctx+ "/sys/manager/adduserrole",  data:formdata,  type: ' Post ',  dataType: "JSON",      Processdata:false,      contenttype:false,  success:function (result) {  if ( Result!=null) {  testcallback ();}}  );} function Testcallback () {alert (1) test2 ()}function test2 () {alert (2)}

This is the end of this article (want to see more on the Topic.alibabacloud.comAJAX User manual section of the study), there are questions can be in the message below the question.

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.