I would like make a countDown with Angular js. this is my code:
Html File
{{countDown}}
function timerCtrl ($scope,$interval) {
$scope.seconds = 0;
var timer = $interval(function(){
$scope.seconds++;
$scope.$apply();
console.log($scope.countDown);
}, 1000);
}
The way I did , it works!
Angular code
var app = angular.module('counter', []);
app.controller('MainCtrl', function($scope, $interval) {
var decreamentCountdown = function() {
$scope.countdown -= 1;
if ($scope.countdown < 1) {
$scope.message = "timed out";
}
};
var startCountDown = function() {
$interval(decreamentCountdown, 1000, $scope.countdown)
};
$scope.countdown = 100;
startCountDown();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.min.js"></script>
<body ng-app="counter" ng-controller="MainCtrl">
{{countdown}} {{message}}
</body>
$scope.countDown = 30;
var timer;
$scope.countTimer = function () {
var time = $timeout(function () {
timer = setInterval(function () {
if ($scope.countDown > 0) {
$scope.countDown--;
} else {
clearInterval(timer);
$window.location.href = '/Logoff';
}
$scope.$apply();
}, 1000);
}, 0);
}
$scope.stop= function () {
clearInterval(timer);
}
IN HTML:
<button type="submit" ng-click="countTimer()">Start</button>
<button type="submit" ng-click="stop()">Clear</button>
Please take a look at this example here. It is a simple example of a count up! Which I think you could easily modify to create a count down.
http://jsfiddle.net/ganarajpr/LQGE2/
function AlbumCtrl($scope,$timeout) {
$scope.counter = 0;
$scope.onTimeout = function(){
$scope.counter++;
mytimeout = $timeout($scope.onTimeout,1000);
}
var mytimeout = $timeout($scope.onTimeout,1000);
$scope.stop = function(){
$timeout.cancel(mytimeout);
}
}
<!doctype html>
<html ng-app>
<head>
<script src="http://code.angularjs.org/angular-1.0.0rc11.min.js"></script>
<script src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
</head>
<body>
<div ng-controller="AlbumCtrl">
{{counter}}
<button ng-click="stop()">Stop</button>
</div>
</body>
</html>