var app = angular.module('quizApp', []);
app.directive('timer', function($timeout, $compile) {
return {
restrict: 'E',
replace: false,
scope: {
interval: '=interval',
startTimeAttr: '=startTime',
countdownattr: '=countdown'
},
controller: function($scope, $element) {
if ($element.html().trim().length === 0) {
$element.append($compile('<span>{{millis}}</span>')($scope));
}
$scope.startTime = null;
$scope.timeoutId = null;
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
$scope.isRunning = false;
$scope.$on('timer-start', function() {
$scope.start();
});
$scope.$on('timer-resume', function() {
$scope.resume();
});
$scope.$on('timer-stop', function() {
$scope.stop();
});
function resetTimeout() {
if ($scope.timeoutId) {
clearTimeout($scope.timeoutId);
}
}
$scope.start = $element[0].start = function() {
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
resetTimeout();
tick();
};
$scope.resume = $element[0].resume = function() {
resetTimeout();
$scope.startTime = new Date() - ($scope.stoppedTime - $scope.startTime);
tick();
};
$scope.stop = $element[0].stop = function() {
$scope.stoppedTime = new Date();
resetTimeout();
$scope.timeoutId = null;
};
$element.bind('$destroy', function() {
resetTimeout();
});
var tick = function() {
if ($scope.countdown > 0) {
$scope.countdown--;
} else if ($scope.countdown <= 0) {
$scope.stop();
}
$scope.millis = new Date() - $scope.startTime;
if ($scope.countdown > 0) {
$scope.millis = $scope.countdown * 1000
}
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
$scope.minutes = Math.floor((($scope.millis / (1000 * 60)) % 60));
$scope.hours = Math.floor((($scope.millis / (1000 * 60 * 60)) % 24));
$scope.days = Math.floor((($scope.millis / (1000 * 60 * 60)) / 24));
//We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
console.log($scope.seconds)
$scope.timeoutId = setTimeout(function() {
tick();
$scope.$apply();
}, $scope.interval);
$scope.$emit('timer-tick', {
timeoutId: $scope.timeoutId,
millis: $scope.millis
});
};
$scope.start();
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="quizApp">
<timer interval="1000">Time ends in : {{hours}} hours, {{minutes}} minutes, {{seconds}} seconds</timer>
</div>
I have this timer directive and got some issues listed below. Couldn't able to figure it out. Any help would be appreciated.
1) hours, minutes and seconds are not getting updated in markup.
2) timer is in increasing order. I want it in decreasing order.
I'm using this timer in my quiz app. So let's say total time is 1 hr 54mins 00sec. On starting quiz timer should start decreasing by seconds and then minutes and then hours. When it becomes 0, I should get an alert.
Thank you in advance.