Javascript 如何每10秒调用一个JS函数,然后在angular中激活一个函数
我试图使用highcharts图表,我想“模拟”实时数据输入,因此,当用户按下“启动实时流”按钮时,它会激活一个函数,我想是网页上的JavaScript,然后调用角度控制器函数,该函数有大约10秒的延迟 我可以从控制器查询json数据的方式是从http请求,我使用了我想要查询数据的时间(我有100周的时间)。所以我想在网页上有一个函数,从99和100开始,将变量传递给角度函数,以查询100-99周前的数据,并将数据添加到图表中。等待10秒,现在查询99-98,直到它变为零 一般来说,我对JS非常陌生,所以我不太确定如何启动,但我已经阅读了关于setTimeout函数的内容。如有任何建议或更好的方法,我们将不胜感激 我当前的http请求如下所示,是静态的:Javascript 如何每10秒调用一个JS函数,然后在angular中激活一个函数,javascript,angularjs,highcharts,Javascript,Angularjs,Highcharts,我试图使用highcharts图表,我想“模拟”实时数据输入,因此,当用户按下“启动实时流”按钮时,它会激活一个函数,我想是网页上的JavaScript,然后调用角度控制器函数,该函数有大约10秒的延迟 我可以从控制器查询json数据的方式是从http请求,我使用了我想要查询数据的时间(我有100周的时间)。所以我想在网页上有一个函数,从99和100开始,将变量传递给角度函数,以查询100-99周前的数据,并将数据添加到图表中。等待10秒,现在查询99-98,直到它变为零 一般来说,我对JS非常
$http({
url: '/api/v1/datapoints',
method: 'POST',
data: '{"start":"99w-ago","end":"98w-ago","tags":[{"name":"SolarData"}]}'
}).then(function(predixTimeSeriesData){
$scope.solarData = predixTimeSeriesData.data.tags[0].results[0].values.map(
function(curVal, index, arr) {
return [curVal[0], curVal[1]];
}
);
console.log($scope.solarData);
/*
I use $scope.solatData in my chart on the html page like
<line-series-chart data={{solarData}}></line-series-chart>
so this is why I am thinking I need to have the time interval on the view page
instead of the controller because i cannot control my chart from there
*/
});
$http({
url:“/api/v1/datapoints”,
方法:“POST”,
数据:“{“开始”:“99w前”,“结束”:“98w前”,“标记”:[{“名称”:“SolarData”}]}”
}).then(函数(predixTimeSeriesData){
$scope.solarData=predixTimeSeriesData.data.tags[0].结果[0].值.map(
函数(曲线、索引、arr){
返回[curVal[0],curVal[1];
}
);
console.log($scope.solarData);
/*
我在html页面的图表中使用$scope.solatData,如下所示
这就是为什么我认为我需要在查看页面上设置时间间隔的原因
而不是控制器,因为我无法从那里控制图表
*/
});
您可以使用angular的$interval
服务,如下所示:
function myController($scope, $http, $interval) {
var currentWeek = 99;
var fetchInterval;
$scope.solatData = [];
$scope.fetch = function() {
$http.get("someUrl", {
params: {
week: currentWeek
}
}).then(function(data){
// This will also update your graph, assuming it is implemented
// to watch changes on the data
$scope.solatData = $scope.solatData.concat(data);
currentWeek++;
});
}
$scope.start = function() {
fetchInterval = $interval($scope.fetch, 10000);
}
// Clear the interval when the scope/controller is 'destroyed'
$scope.$on('$destroy', function() {
$interval.cancel(fetchInterval);
});
// kick off initial start
$scope.start();
}
您可以使用angular的
$interval
服务,如下所示:
function myController($scope, $http, $interval) {
var currentWeek = 99;
var fetchInterval;
$scope.solatData = [];
$scope.fetch = function() {
$http.get("someUrl", {
params: {
week: currentWeek
}
}).then(function(data){
// This will also update your graph, assuming it is implemented
// to watch changes on the data
$scope.solatData = $scope.solatData.concat(data);
currentWeek++;
});
}
$scope.start = function() {
fetchInterval = $interval($scope.fetch, 10000);
}
// Clear the interval when the scope/controller is 'destroyed'
$scope.$on('$destroy', function() {
$interval.cancel(fetchInterval);
});
// kick off initial start
$scope.start();
}
使用Angular的和/或服务使用Angular的和/或服务我也必须通过html页面调用函数吗?它当前不起作用。您必须使用
$scope.start()
取消间隔,我是否也必须通过html页面调用该函数?它当前不工作。您必须使用$scope.start()