diff --git a/app/js/timer.js b/app/js/timer.js index ced1501..5677b23 100644 --- a/app/js/timer.js +++ b/app/js/timer.js @@ -1,3 +1,10 @@ +/** + * angular-timer - v1.1.6 - 2014-07-01 7:37 AM + * https://github.com/siddii/angular-timer + * + * Copyright (c) 2014 Siddique Hameed + * Licensed MIT + */ var timerModule = angular.module('timer', []) .directive('timer', ['$compile', function ($compile) { return { @@ -8,11 +15,12 @@ var timerModule = angular.module('timer', []) startTimeAttr: '=startTime', endTimeAttr: '=endTime', countdownattr: '=countdown', + finishCallback: '&finishCallback', autoStart: '&autoStart', maxTimeUnit: '=' }, controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) { - + console.log($scope.interval); // Checking for trim function since IE8 doesn't have it // If not a function, create tirm with RegEx to mimic native trim if (typeof String.prototype.trim !== 'function') { @@ -58,6 +66,14 @@ var timerModule = angular.module('timer', []) $scope.countdown = countdown; }); + $scope.$on('timer-set-value', function (e, seconds) { + if ($scope.countdownattr) { + $scope.countdown = $scope.countdown - ($scope.millis / 1000 - seconds); + } else { + $scope.startTime = new Date() - (seconds * 1000); + } + }); + function resetTimeout() { if ($scope.timeoutId) { clearTimeout($scope.timeoutId); @@ -212,13 +228,16 @@ var timerModule = angular.module('timer', []) if ($scope.countdownattr) { - $scope.millis = $scope.countdown * 1000; + $scope.millis = Math.round(($scope.countdown * 1000 - $scope.millis) / 1000) * 1000; } if ($scope.millis < 0) { $scope.stop(); $scope.millis = 0; calculateTimeUnits(); + if($scope.finishCallback) { + $scope.$eval($scope.finishCallback); + } return; } calculateTimeUnits(); @@ -230,13 +249,6 @@ var timerModule = angular.module('timer', []) }, $scope.interval - adjustment); $scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis}); - - if ($scope.countdown > 0) { - $scope.countdown--; - } - else if ($scope.countdown <= 0) { - $scope.stop(); - } }; if ($scope.autoStart === undefined || $scope.autoStart === true) { diff --git a/dist/angular-timer.js b/dist/angular-timer.js index 7b11bd1..48a42a0 100644 --- a/dist/angular-timer.js +++ b/dist/angular-timer.js @@ -1,5 +1,5 @@ /** - * angular-timer - v1.1.5 - 2014-06-14 7:52 AM + * angular-timer - v1.1.6 - 2014-07-01 7:37 AM * https://github.com/siddii/angular-timer * * Copyright (c) 2014 Siddique Hameed @@ -15,11 +15,12 @@ var timerModule = angular.module('timer', []) startTimeAttr: '=startTime', endTimeAttr: '=endTime', countdownattr: '=countdown', + finishCallback: '&finishCallback', autoStart: '&autoStart', maxTimeUnit: '=' }, controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) { - + console.log($scope.interval); // Checking for trim function since IE8 doesn't have it // If not a function, create tirm with RegEx to mimic native trim if (typeof String.prototype.trim !== 'function') { @@ -65,6 +66,14 @@ var timerModule = angular.module('timer', []) $scope.countdown = countdown; }); + $scope.$on('timer-set-value', function (e, seconds) { + if ($scope.countdownattr) { + $scope.countdown = $scope.countdown = $scope.countdown - ($scope.millis / 1000 - seconds); + } else { + $scope.startTime = new Date() - (seconds * 1000); + } + }); + function resetTimeout() { if ($scope.timeoutId) { clearTimeout($scope.timeoutId); @@ -219,13 +228,16 @@ var timerModule = angular.module('timer', []) if ($scope.countdownattr) { - $scope.millis = $scope.countdown * 1000; + $scope.millis = Math.round(($scope.countdown * 1000 - $scope.millis) / 1000) * 1000; } if ($scope.millis < 0) { $scope.stop(); $scope.millis = 0; calculateTimeUnits(); + if($scope.finishCallback) { + $scope.$eval($scope.finishCallback); + } return; } calculateTimeUnits(); @@ -237,13 +249,6 @@ var timerModule = angular.module('timer', []) }, $scope.interval - adjustment); $scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis}); - - if ($scope.countdown > 0) { - $scope.countdown--; - } - else if ($scope.countdown <= 0) { - $scope.stop(); - } }; if ($scope.autoStart === undefined || $scope.autoStart === true) {