jquery callback function only working on last loop

前端 未结 4 1133
我寻月下人不归
我寻月下人不归 2020-11-29 08:39
for (var i = 0; i < barValues.length; i++) {


    actualBarHeight = Math.floor((barValues[i] / chartMaxY) * barchartHeight);

    var barChartID = \"#barChart\"          


        
4条回答
  •  粉色の甜心
    2020-11-29 09:23

    Since you are using jQuery you can use the $.each function instead of a for loop. The callback used instead of the loop will create a new closure.

    $.each(barValues, function(i, barValue){
        actualBarHeight = Math.floor((barValue/chartMaxY)*barchartHeight);
    
        var barChartID = "#barChart" + (i+1)
        $(barChartID + " .value span").css('background-color','transparent');
        $(barChartID + " img").animate({ 
                        height: actualBarHeight
                }, 500, function(){$(barChartID + " .value span").css('background-color','white');}
        );
    
        $(barChartID + " .value span").html("$"+Math.floor(barValue));
        $(barChartID + " .value").css("bottom",actualBarHeight+"px");
        $(barChartID + " .ylabel").html(chartMaxY);
    });
    

提交回复
热议问题