Wait until all jQuery Ajax requests are done?

2018-12-31 01:00发布

How do I make a function wait until all jQuery Ajax requests are done inside another function?

In short, I need to wait for all Ajax requests to be done before I execute the next. But how?

21条回答
旧人旧事旧时光
2楼-- · 2018-12-31 01:21

I have met this problem and created a generic plugin jquery_counter to solve it: https://bitbucket.org/stxnext/jquery_counter/

查看更多
一个人的天荒地老
3楼-- · 2018-12-31 01:23

jQuery allows you to specify if you want the ajax request to be asynchronous or not. You can simply make the ajax requests synchronous and then the rest of the code won't execute until they return.

For example:

jQuery.ajax({ 
    async: false,
    //code
});
查看更多
零度萤火
4楼-- · 2018-12-31 01:25

$.when doesn't work for me, callback(x) instead of return x worked as described here: https://stackoverflow.com/a/13455253/10357604

查看更多
登录 后发表回答