Coordination of Ajax data sources

Source: Internet
Author: User

For various reasons, we sometimes coordinate two or more Ajax data sources. What should we do in this case?

 

First, define these data sources as global variables, assign values to corresponding global data source variables in their Ajax callback functions, and then judge the data sources used in the final public coordination function. During the judgment process, if these global variables are not initialized, it means that some Ajax calls have not yet been returned or the value assignment to the data source has not been completed yet. In this case, the system must stop executing the function.Code, Wait for a certain time interval, enter the function again, and then judge the data source again (use setTimeout to repeat your calls) until all these data sources are verified, continue to execute the function code. Note: after these data sources are processed, they must be cleared to prepare for the next assignment.

 

In a project demo just completed, I encountered this situation. Since there were only two data sources to be coordinated at that time, and the time was not sufficient, I did not perform in-depth analysis and directly adopted the stupid method: add judgment on another data source (both data sources are declared as global variables) in their respective functions: if the other data source is not assigned a value, then ......; if the value has been assigned, then ....... Now I want to think about it. This practice can only be said that I have a little idea of coordination and handling, and I am not in depth thinking: I have not extracted my own judgments for further processing.

 

This time, we will record the handling ideas by referring to other people's practices and follow these ideas for future use.

 

[Supplement]

A colleague's solution is to add some public variables and add a public function to process and judge these variables. If all variables are assigned values, continue. Otherwise, the variable jumps out. Add the call to this public function in each Ajax callback function.

Another idea: You can also serialize Ajax calls, that is, add a callback to another Ajax call in an Ajax callback function.

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.