开发者

Collect data from multiple sources

开发者 https://www.devze.com 2023-03-27 13:08 出处:网络
I have a need to collect data from multiple sources (xml or JSON format) via AJAX, and then run a function that consumes these data sets.

I have a need to collect data from multiple sources (xml or JSON format) via AJAX, and then run a function that consumes these data sets.

How can I check that all the AJAX calls are successful before the function is executed? Ideally, I'开发者_JAVA百科d like to keep async calls to minimize the response time.


Conceptually, it would work like this:

  1. Use async calls.
  2. Code a success handler for each call.
  3. When a call succeeds, you store the data from it in some common place and then record that this call has finished.
  4. At the end of each success handler, you check to see if all data is available now and, if so, you can a function to consume it.

Some pseudo code for an ajax call:

results = [];
$.ajax({
   // other parameters here
    success: function(data) {
        results.push(data);
        checkResults();
    };
});

function checkResults() {
    // if all five results are in, then we're ready to process them
    if (results.length >= 5) {
         processResults(results);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号