如果打开新组件,功能setTimeout
的服务继续工作。
//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
}); }
将timeoutID存储在全局变量(或共享服务)中,然后在需要停止执行clearTimeout
时将其传递给setTimeout。