我是 angularjs 的初学者,如果我提出了愚蠢的问题,请原谅。
function getCams(callback){
var media_list = [];
MediaStreamTrack.getSources(function(sourceInfos){
var i=0;
while(i!=sourceInfos.length){
if (sourceInfos[i].kind == 'video'){
var temp = [];
temp.push(sourceInfos[i].id);
temp.push(sourceInfos[i].label);
media_list.push(temp);
}
i++;
}
callback(media_list);
});
}
var app = angular.module('myApp', []).config(function($interpolateProvider){
$interpolateProvider.startSymbol('{[{').endSymbol('}]}');
});
app.controller('myCtrl', function($scope, $interval) {
$scope.cams = [];
var checkcams = getCams(function(media_list){
$scope.cams=media_list;
$scope.$apply();
console.log("test");
});
$interval(checkcams, 10000);
});
上面是我试图获取连接到系统的摄像头数量的代码,并尝试使用回调函数在 Angular js 中更新相同的代码,在这一行中
$interval(checkcams, 10000);
我尝试每 10 秒调用一次该函数,但该函数仅在页面加载后运行一次,而不是每 10 秒运行一次。
我已经看过这个问题了,它不会帮助我解决问题。 $interval not running, angularjs
最佳答案
getCams
没有返回任何内容,因此 $interval
不工作。这是预期的行为。
您可以将代码重写为
//Wrap getCams call in a function
var checkcams = function(){
getCams(function(media_list){
$scope.cams=media_list;
$scope.$apply();
console.log("test");
});
}
//Call it when view is launched
checkcams();
//set Interval
$interval(checkcams, 10000);
关于javascript - $interval 不适用于回调函数,angular js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32349898/