patricia bezek patricia bezek - 17 days ago 6
AngularJS Question

how can i make an angular countdown in minutes?

Here is my code but I can't find how to make the countdown in minutes, I mean, instead of 3000, 30:00... any help?

<script>
app.controller("Counter", function($scope,$timeout){
$scope.counter = 3000;
$scope.onTimeout = function(){
$scope.counter--;
mytimeout = $timeout($scope.onTimeout,1000);
}
var mytimeout = $timeout($scope.onTimeout,1000);
});
</script>

Answer

Your controller is OK, only change 3000 to 1800 (30 minutes is 1800 seconds). Now to display seconds as 'mm:ss' convert counter to date. You can use filter (from this answer)

app.filter('secondsToDateTime', [function() {
    return function(seconds) {
        return new Date(1970, 0, 1).setSeconds(seconds);
    };
}])

and display it like that:

{{counter | secondsToDateTime | date:'mm:ss'}}

See how it works in fiddle.

Comments