Countdown Timer in AngularJS using SVG
Posted By : Milind Ahuja | 11-Jun-2016
A great countdown timer with AngularJS module that uses SVG to create a circular progressbar and the timer at the center of this circular progressbar.
First of all you need to install the following AngularJS module Angular SVG round progressbar that uses SVG: https://github.com/crisbeto/angular-svg-round-progressbar. Include angular and all the necessary files with dependency injection.
Secondly, you must have Humanize Duration and Moment which are used by the abouve Module and are for parsing, validating, manipulating and to convert date in required format. Click on the following links to install both the modules:
https://github.com/siddii/angular-timer/tree/master/bower_components/humanize-duration
https://github.com/moment/moment
With the help of following code, you can easily convert a simple progress into a countdown Timer.
HTML:
{{humanizeDurationTimer(timer,'s')}}
CSS:
.progress-wrapper {
position: relative;
margin: 2px auto;
font-size: 21px;
}
.progress-wrapper path {
stroke: #4a62ad !important;
}
.progress {
position: absolute;
width: 100%;
height: 100% !important;
line-height: 9em;
font-size: 1em;
text-align: center;
font-weight: 100;
background-color: #fff !important;
box-shadow: none !important;
}
.round-progress-wrapper {
text-align: center;
margin-bottom: 25px
}
JS(Functions to control the timer):
// starts the timer
$scope.startTimer = function() {
mytimeout = $timeout($scope.onTimeout, 1000);
$scope.started = true;
$scope.selectTimer(600);
};
// This function helps to display the time in a correct way in the center of the timer
$scope.humanizeDurationTimer = function(input, units) {
// units is a string with possible values of y, M, w, d, h, m, s, ms
if (input == 0) {
return 0;
} else {
var duration = moment().startOf('day').add(input, units);
var format = "";
if (duration.hour() > 0) {
format += "H[h] ";
}
if (duration.minute() > 0) {
format += "m[m] ";
}
if (duration.second() > 0) {
format += "s[s] ";
}
return duration.format(format);
}
};
// stops and resets the current timer
$scope.stopTimer = function(closingModal) {
if (closingModal != true) {
$scope.$broadcast('timer-stopped', $scope.timer);
}
$scope.timer = $scope.timeForTimer;
$scope.started = false;
$scope.paused = false;
$timeout.cancel(mytimeout);
};
// When you open a timer page or modal, this function is called.
//This function also makes the progressbar move along with the countdown timer.
$scope.selectTimer = function(val) {
var timeDiff = 600 - val;
$scope.timeForTimer = timeDiff + val;
$scope.timer = val
$scope.started = false;
$scope.paused = false;
$scope.done = false;
};
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Milind Ahuja
Milind is a bright Lead Frontend Developer and have knowledge of HTML, CSS, JavaScript, AngularJS, Angular 2+ Versions and photoshop. His hobbies are learning new computer techniques, fitness and interest in different languages.