Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value setter for timer #122

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions app/js/timer.js
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/siddii/angular-timer/blob/master/LICENSE.txt>
*/
var timerModule = angular.module('timer', [])
.directive('timer', ['$compile', function ($compile) {
return {
Expand All @@ -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') {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
25 changes: 15 additions & 10 deletions dist/angular-timer.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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') {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down