setTimeout(function(){
userIsSetup = true;
console.log('This happened!');
}, 4000);
$interval(function(){
if(userIsSetup){
$scope.received = true;
$interval.cancel(); // Can pass a promise into this as well
}
console.log('A second has passed');
}, 1000, 30);
这是对未来功能的一种测试,但基本上我只想查看是否每秒执行一次,持续30秒。如果完成,请停止计时器并打开一个变量,以便页面上的某些内容消失,否则在三十秒后告诉用户需要一些时间。
正如标题所说,控制台登录时间间隔只发生一次,当页面加载时,然后不再发生。我是否需要以某种方式启动计时器?
答案 0 :(得分:1)
尝试这样的事情。
$scope.userIsSetup = false;
$interval = setInterval(function(){
$scope.check();
console.log('This happened!');
},2000);
$scope.check = function(){
if($scope.userIsSetup){
$scope.received = true;
$interval.cancel();
}
console.log('2 second has passed');
};
对于角度
中的$ intervalapp.controller('MainCtrl', function($scope,$interval) {
$scope.totalTimepassed = 0;
$scope.callAtInterval = function () {
if($scope.totalTimepassed < 9)
{
$scope.totalTimepassed = $scope.totalTimepassed +1;
console.log($scope.totalTimepassed +" seconds passed");
}
else
{
$interval.cancel(interval);
console.log("10 seconds passed");
}
}
var interval = $interval($scope.callAtInterval, 1000);
});