Highcharts:当我打开新的组件角度时,如何停止函数setTimeout?

问题描述 投票:0回答:1

如果打开新组件,功能setTimeout的服务继续工作。

现场演示:http://next.plnkr.co/edit/OqMofEGDadF9J3Uit8qD?utm_source=legacy&utm_medium=worker&utm_campaign=next&preview

//simplified value generator   
function requestData() {

$.ajax({
url: 'points.json',
success: function(seriesUpdate) {

  //in case initializer of highcharts is too quick - skip the update
  if (!chart) {
    setTimeout(requestData, 1000);
    return;
  }

  //mocking for making static response data into dynamic
  $.each(seriesUpdate, function (serieIndex, serieUpdate) {
    mockPointCoordinates(serieUpdate.data[0]);
  });

  $.each(seriesUpdate, function (serieIndex, serieUpdate) {
        var existingSerie = chartSeries[serieUpdate.name];
        if (existingSerie ) {
          //add a point
          var shift = existingSerie.data.length > 20;
          existingSerie.addPoint(serieUpdate.data[0], true, shift);
        } else {
          //add a chart with point
              var newSerie = chart.addSeries({                        
                name: serieUpdate.name,
                  data: serieUpdate.data
                }, true);
                chartSeries[serieUpdate.name] = newSerie;
        }

      });
  // call it again after one second
  setTimeout(requestData, 1000);
},
cache: false

}); }

javascript angular typescript highcharts
1个回答
0
投票

timeoutID存储在全局变量(或共享服务)中,然后在需要停止执行clearTimeout时将其传递给setTimeout

© www.soinside.com 2019 - 2024. All rights reserved.