Sometimes in a business event processing process, you may encounter a click of a button or other event that triggers an action.
Requires more than two AJAX requests, but may be concerned with the sequencing of Ajax requests, and sometimes the order of Ajax requests can lead to a variety of problems
For example, there are now two Ajax events, AJAX1, AJAX2
A method called Main to execute the portal
1.
function Main () {
ajax1 (data,callback);
AJAX2 (Data,callback);
}
If we follow the above method, it appears that the AJAX1 should be executed first, ajax2 after execution. If you don't think about it, some people think that Ajax1 is done first and then executes AJAX2. Is this really going to happen?
The answer is not necessarily. Of course, we don't need to think too much about who executes first, who returns first, for situations where multiple AJAX requests do not require a return order.
What about the sequence and return order of Ajax events?
If so, how do you solve the Ajax event's execution return order?
Of course now you should think of Ajax callback functions, good, which is a good idea
Now change the method by this way, as follows
function Main () {
ajax1 (data, AJAX2 ());
}
Does it look like you think very good? Are you sure? Is it really the very good?
Maybe,but in some case, this may be inconvenient to write, of course, maybe you may not encounter, but I encountered a very special situation
such as the following possible
function Main () {
AA (data);
Ajax1 (Data,callback);
}
Function AA (val) {
var data=val+ "";//This is to modify the incoming data, encapsulation, of course, this is casually written
ajax2 (data,ajax2callback);
}
function Ajax2callback () {
console.log ("===== callback function Ajax2callback () execute ========");
Console.log (the action that must be performed after Ajax execution is ===== here ======== ");
}
Do you think this situation can satisfy ajax2 in AJAX1 first and execute first?
Think about it and you'll find the answer.
No!
Now, you solve this proplem. How to make sure Ajax2 finished before ajax1.
Of course, you might say, it's not easy to put AJAX1 calls into the final face of the Ajax2 method callback method Ajax2callback
I have to admit that this is a solution, but if it is a very old project, it has been a project for several years. Inside the call is complex, try to avoid modifying the previous low-level methods, perhaps you in order to modify the bug, so simple to solve, you may create multiple bugs.
Is there any good way to solve it?
Sure,solve it easy. But inexperienced people rarely think of it, and only use the previous approach to solve problems hastily, no matter 3,721.
And I used a relatively stupid method, and there are some problems, using the settimeout timer to perform once, but the problem must be everyone knows, who knows how long this Ajax will be carried out, but fortunately there is great God under the guidance of me,
Remember to sort the array, say this, and perhaps you'll be curious as to how the arrays are sorted. I'll tell you the answer here.
The code illustrates everything:
function Main () {
var temp=ajax2callback;
Ajax2callback=function () {
temp ();
Ajax1 (Data,callback);
}
AA (data);
ajax2callback=temp;
}
Function AA (val) {
var data=val+ "";//This is to modify the incoming data, encapsulation, of course, this is casually written
ajax2 (data,ajax2callback);
}
function Ajax2callback () {
console.log ("===== callback function Ajax2callback () execute ========");
Console.log (the action that must be performed after Ajax execution is ===== here ======== ");
}
See not, is not very interesting, did not modify the lowest layer method, only modified the main method, is not very much like the array in the sort of when we compare two value sizes, whether you use bubble sort, or quick sort, is not set a temporary variable to store the value. Of course, when you sort the comparison size, you may not need to set the temporary variable, just use a ^ operator to do the assignment size, even you can lazy to call the system directly Arrays.sort () method, of course, this can
function Changesearchcontacttype (obj) {if (!obj) {return;}
var contacttype = Obj.value;
var origrendertemplate = rendertemplate; Rendertemplate = function (data) {origrendertemplate (data); Ajaxanywhere.submitajax (' setsearchcontacttype '); var
result = Templatehelper.changesearchcontacttemplate (ContactType, Contactuiuid);
Rendertemplate = origrendertemplate;
return result; }