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.