我有一个简单的控制器,可以进行简单的倒计时。
当我运行此代码时,我得到的只是控制台中的一个“勾号”。我希望每5毫秒就有一个滴答。
.controller('Countdown', function($scope, $timeout){
$scope.endtime = new Date('May 5, 2014 00:00:00');
var countup = function() {
$scope.currtime = new Date();
$scope.timeleft = $scope.endtime-$scope.currtime;
console.log('tick');
};
$timeout(countup, 5);
});
答案 0 :(得分:3)
正如迈克尔所说,你应该使用 $ interval
或者你可以这样做。
.controller('Countdown', function($scope, $timeout){
$scope.endtime = new Date('May 5, 2014 00:00:00');
var countup = function() {
$scope.currtime = new Date();
$scope.timeleft = $scope.endtime-$scope.currtime;
console.log('tick');
$timeout(countup, 5); // Recursive technique
};
});
答案 1 :(得分:1)
$timeout
是setTimeout包装器,你想要的是设置间隔