如果async.jquery

时间:2017-12-29 06:11:49

标签: javascript jquery asynchronous async.js google-chartwrapper

我正在使用asyncjs来加载Google图表,它运行正常,但有时api从中获取图表的数字会返回空值并停止加载更多图表。

这是我的代码

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变量,有时它返回null并且进一步的迭代停止。

我试图设置

 if (data == null)
     return;

   if (data == null)
     return true;

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

1 个答案:

答案 0 :(得分:4)

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

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