multiple ajax calls wait for last one to load, then execute

前端 未结 3 1640
栀梦
栀梦 2020-12-14 09:32

I have multiple ajax queries running at the same time, and I want them to wait for the last one to return, and then run the success handler on all of the ajax calls. For a

3条回答
  •  天涯浪人
    2020-12-14 10:12

    This can probably be done in a very similar fashion to what you've already proposed.

    First, create global variables var sleep = true and var request_count = 0. Then start a timer that checks request_count against total number of requests every second or so. Then, your success functions can increment request_counter and then start looping until sleep == false. Then, when the monitoring function running via timer detects request_count == TOTAL_REQUESTS it sets sleep = false, and all your success functions continue about their business at that point.

提交回复
热议问题