我有两个数组选择1和选择2。这些数组中的项目是我发送的 $.get() 属性。我正在尝试在循环完成后渲染 Highcharts 图表(使用 renderChart() 调用)函数。我尝试了多种解决方案,最终得到了下面的代码。我如何监听所有 $.get() 请求何时完成,以便我可以触发页面上的 Highcharts 函数?
var chosen1 = [],
chosen2 = [],
computedSeries = [],
XHRs = [];
function getData() {
var rebuildingChart = '<div id="rebuilding-chart"><div id="floatingCirclesG"><div class="f_circleG" id="frotateG_01"></div><div class="f_circleG" id="frotateG_02"></div><div class="f_circleG" id="frotateG_03"></div><div class="f_circleG" id="frotateG_04"></div><div class="f_circleG" id="frotateG_05"></div><div class="f_circleG" id="frotateG_06"></div><div class="f_circleG" id="frotateG_07"></div><div class="f_circleG" id="frotateG_08"></div></div><p>Crunching Data</p></div>';
$('#cc-chart').children().remove();
$('#cc-chart').html(rebuildingChart);
$(chosen1).each(function() {
var fruits = this;
$(chosen2).each(function() {
var veggies = this;
XHRs.push($.get("/fetchData", {fruit: fruits, veggie: veggies}, function(data) {
var chartJSON = data,
selectedKPI = veggies,
singleSeries = {},
KPIData = [];
for (var i = 0; i < chartJSON.length; i++) {
var point = [];
point.push(chartJSON[i]['utc_time']);
point.push(Math.round(chartJSON[i]['kpi_value']));
KPIData.push(point);
};
singleSeries["name"] = selectedKPI;
singleSeries["data"] = KPIData;
computedSeries.push(singleSeries);
}));
});
});
$.when(XHRs).then(function() {
renderChart();
});
}
最佳答案
$.when
以 deferreds 作为参数,如果要传递数组,则需要使用 apply
调用该函数:
$.when.apply($, XHRs).then(...)
关于javascript - 如何在使用 $.get() 请求的多维 for 循环后触发事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25395341/