So I'm creating a simple countdown function in AngularJS which is mean to count down to the time set in inputs. The problem is that I'm not able to loop the $scope.countdown
function. So it only counts down once (and if I put the $timeout($scope.countdown(), 1000);
in - it breaks entirely.) I just want it to loop every second until it reaches 0.
Here's my controller:
function countdownController($scope) {
$scope.hours = 2;
$scope.minutes = 30;
$scope.countdown = function(){
if($scope.minutes == 0 && $scope.hours == 0){
alert('end!');
return;
}
else{
if($scope.minutes == 0){
$scope.minutes = 59;
$scope.hours--;
}
else{
$scope.minutes--;
}
}
$timeout($scope.countdown(), 1000); //if I remove this - the numbers display - this actually breaks that.
}
$scope.countdown();
}