如何知道一组getJSON()请求何时完成? [英] How to know when a collection of getJSON() requests are finished?

查看:65
本文介绍了如何知道一组getJSON()请求何时完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道所有这些请求何时完成?

How to know when all of these requests are finished?

$.each(data.response.docs, function(i, item) {
    $.getJSON("jsp/informations.jsp?id=" + item.id, {}, function(data) {..});
});

推荐答案

每个请求完成后,您可以维护一个计数器,但这有点丑陋且不雅致.相反,您可以使用map()将从$.getJSON返回的延迟对象放入数组中.然后可以将比数组应用于$.when.像这样:

You could maintain a counter when each request completes, but that's a bit ugly and inelegant. Instead you could put the deferred objects returned from $.getJSON in to an array using map(). You can then apply than array to $.when. Something like this:

var requests = $.map(data.response.docs, function(i, item) {
    return $.getJSON('jsp/informations.jsp', { id: item.id }, function(data) {
        // ...
    });
}).get();

$.when.apply(requests).done(function() {
    // all requests complete...
});

这篇关于如何知道一组getJSON()请求何时完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆