How can I make batches of ajax requests in jQuery?

前端 未结 4 2048
半阙折子戏
半阙折子戏 2020-12-05 05:35

I\'m wondering how to make ajax calls in groups of n.

Here\'s my use case:

I have a table that displays usage data. You can drill into each row, and if eac

4条回答
  •  醉梦人生
    2020-12-05 05:44

    You can take a look at using jQuery.when, which allows you to execute callback functions when all requests have completed.

    $.when($.ajax("request1"), $.ajax("request2"), $.ajax("request3"))
     .done(function(data1,  data2, data3){
             // Do something with the data
     });
    

    Or

    $.when($.ajax("request1"), $.ajax("request2"), $.ajax("request3"))
    .then(successCallback, errorHandler);
    

    See the following post for more information.

    Also, I'm not sure that your apprehension towards using a plugin should be affected by the fact that you are in a work environment, especially if it simplifies your work. Hence allowing to be more productive. Granted you have to choose your plugins carefully, since quality and long term maintenance can be a issue.

提交回复
热议问题