In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to solve the problem of multiple Ajax requests returning in sequence". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem of multiple Ajax requests returning successively".
Sometimes in a business event handling process, you may encounter clicking a button or other event triggering an action
You need to execute more than two Ajax requests, but you may have to consider the order in which Ajax requests are executed. Sometimes there is something wrong with the order of Ajax requests, which will lead to various problems.
For example, there are now two ajax events, ajax1 and ajax2
A method call execution entry called main
1.
Function main () {ajax1 (data,callback); ajax2 (data,callback);}
If we follow the above method, on the surface, it seems to let ajax1 execute first and ajax2 later. If you don't think about it carefully, some people will think that ajax1 will be executed before ajax2. In fact, will it really be so?
The answer is not necessarily. Of course, for cases where there are multiple ajax requests that do not require a return order, we do not need to think too much about who executes first and who returns first.
What if we consider the sequence of execution and return order of ajax events?
If so, how to solve the order of return of ajax events?
Of course, now you should think of the ajax callback function, Good, which is a good idea.
Now change the method according to this idea, the method is as follows
Function main () {ajax1 (data, ajax2 ();}
In this way, do you think Very Good? Oh yeah? Is it really Very Good?
Maybe,But in some case, it may be a little inconvenient to write like this, and of course you may not encounter it, but I have encountered a rather special situation.
For example, the following possibility
Function main () {aa (data); ajax1 (data,callback);} function aa (val) {var data=val+ "; / / here is the modification and encapsulation of the incoming data, of course, here is the casually written ajax2 (data,ajax2Callback);} function ajax2Callback () {console.log (" = callback function ajax2Callback () execution = "); console.log (" = the operation that must be performed after ajax execution is completed);}
Do you think this situation can satisfy the need for ajax2 to execute first and finish execution first in ajax1?
If you think about it, you will find the answer.
No!
Now, How to solve this proplem. How to make sure ajax2 finished before ajax1.
Of course, you might say, it's not easy to put the call to ajax1 at the end of the callback method ajax2Callback of the ajax2 method.
I have to admit that this is a solution, but if it is a very old project, it has been working on it for several years. The call inside is complex, so try to avoid modifying the previous underlying methods. Maybe if you simply solve the problem in order to modify the bug, you may create more than one bug.
Is there any good way to solve it?
Sure,solve it easy. However, people who are inexperienced in work seldom think of it and will only use the previous method to solve the problem hastily, regardless of three, seven or twenty-one.
And I adopted a rather stupid method, and there are still some problems, using the setTimeOut timer to execute once, but the problem must be known to everyone, who knows how long this Ajax will be executed, fortunately, there is a god to guide me.
Remember sorting arrays? speaking of which, you might wonder what this has to do with array sorting. I will tell you the answer below.
The code says it all:
Function main () {var temp=ajax2Callback;ajax2Callback=function () {temp (); ajax1 (data,callback);} aa (data); ajax2Callback=temp;} function aa (val) {var data=val+ "; / / here is the modification and encapsulation of the incoming data, of course, here is the casually written ajax2 (data,ajax2Callback);} function ajax2Callback () {console.log (" = callback function ajax2Callback () execution = "); console.log (" = the actions that must be performed after ajax is completed = ") }
See, isn't it interesting, there is no way to modify the lowest level, only the main method is modified, is it very similar to the array sort when we compare the size of two values, whether you use bubble sort or quick sort, whether you set a temporary variable to store the value. Of course, when the sort is large, you don't have to set temporary variables, just use an ^ operator to do the assignment ratio, or you can even be lazy enough to call the system's Arrays.sort () method directly, of course.
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 } Thank you for your reading. the above is the content of "how to solve the problem of multiple Ajax requests returning in sequence". After the study of this article, I believe you have a deeper understanding of how to solve the problem of multiple Ajax requests returning in sequence, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.