for(c=0; c < $scope.graphPlotsChunk.length; c++ ){
      if(c == 0){
           Plotly.newPlot(graphRender, [$scope.graphPlots2[c]], $scope.layout2);
      }else{
           setTimeout(function() {
              $scope.testCounter.push(_.clone(c));
              console.log($scope.testCounter);
              Plotly.addTraces(graphRender, $scope.graphPlots2[c]);
           },0);
      }
    }


由于计数器“ c”未同步,对Plotly addTraces的调用将引发错误:
javascript - setTimeout在代码 Angular hack中的位置?-LMLPHP

没有超时,没有错误,但是直到循环结束,跟踪都不会在视图中反映出来。我希望看到跟踪对于大数据绘制是一一出现的,这可能会很慢,所以至少在代码运行时有些变化

有什么建议,不确定从这里去哪里!

最佳答案

使用foreach而不是for循环以某种方式解决了该问题,

$scope.graphPlots2.forEach(function(trace, i) {
    if (i == 0) {
        Plotly.newPlot('graphRender', [trace], $scope.layout2);
    } else {
        setTimeout(function() {
            Plotly.addTraces('graphRender', trace);
        }, 0);
    }
});

关于javascript - setTimeout在代码 Angular hack中的位置?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38252816/

10-12 19:46