如果 async.jquery 中的响应为空,则跳过迭代

Skip iteration if response is null in async.jquery

我正在使用 asyncjs 加载 Google 图表,它工作正常,但有时 api 我从中获取图表 returns 空值和它停止加载更多图表。

这是我的代码

async.eachSeries($('.WeeklyGraph'), function(e, cb){
var ticker = $(e).data('coin');
// console.log(ticker);
$('#'+ticker+'loader').removeClass("hide-sideLoader");
$.getJSON('https://coincap.io/history/7day/'+ticker, function(data){
    // console.log(data);
var month = new Array();
month[0] = "Jan";
month[1] = "Feb";
month[2] = "Mar";
month[3] = "Apr";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7] = "Aug";
month[8] = "Sep";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";
    var CoinPrices = data['price'];
        $.each(CoinPrices, function(index, CoinObject){
            var date = new Date(CoinObject[0]);
            var day = date.getDate();
            var Month = date.getMonth();
            var hours = date.getHours()+ ":" + date.getMinutes();
            CoinObject[0] = day+" "+month[Month]+" "+hours;
        });  
        google.charts.load('current', {packages: ['corechart', 'line']});
        google.charts.setOnLoadCallback(function(){
        var data = new google.visualization.DataTable();
        data.addColumn('string', '');
        data.addColumn('number', '');
        data.addRows(CoinPrices);
        var options = {
            enableInteractivity: false,
           legend: {position: 'none'},
            hAxis: {
            title: '',
            baselineColor: 'none',
            ticks: [],
            gridlines: {color: 'none'}
        },
        vAxis: {
            title: '',
            baselineColor: 'none',
            ticks: [],
            gridlines: {color: 'none'}
        },
            colors: ['#74b42a', '#74b42a']
        };
        var chartID = ticker+'chart_div';
        var chart = new google.visualization.LineChart(document.getElementById(chartID));
        chart.draw(data, options);
        $('#'+ticker+'loader').addClass("hide-sideLoader");
        // $("#chartContent"+coinID).removeClass("hide-chart");
        });

    cb();
});

});

现在我得到响应的 data 变量,有时它 returns 为 null 并且进一步的迭代停止。

我试过设置

 if (data == null)
     return;

   if (data == null)
     return true;

但它会停止所有进一步的迭代。

您应该使用 callback 函数,以转到下一次迭代。即

if(data == null){
     cb(); //in your case, cb is your callback function
 }