Skip to content

Instantly share code, notes, and snippets.

@SuryaSankar
Created February 7, 2015 15:51
Show Gist options
  • Save SuryaSankar/465b8055577ddd3289c6 to your computer and use it in GitHub Desktop.
Save SuryaSankar/465b8055577ddd3289c6 to your computer and use it in GitHub Desktop.
A very simple countdown Timer ( Modified from http://jsfiddle.net/dpeaep/LQGE2/1/ )
angular.module( 'inkmonk.directives', [] )
.directive('inkmonkCountdown',['$compile', function($compile){
return {
restrict: 'AE',
template: "{{counter}}",
scope: {
counterStart : "=start",
dependsOn: "=runduring"
},
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
$scope.counter = parseInt($scope.counterStart);
var stopTimeoutRecursion = false;
$scope.onTimeout = function(){
$scope.counter--;
if ($scope.counter > 0 && !stopTimeoutRecursion) {
$timeout($scope.onTimeout,1000);
}
}
$scope.$watch('dependsOn', function(newValue, oldValue){
if(newValue){
stopTimeoutRecursion = false
$timeout($scope.onTimeout,1000);
}
else{
$scope.counter = $scope.counterStart;
stopTimeoutRecursion = true;
}
});
}]
}
}]);
@SuryaSankar
Copy link
Author

Example Usage:

<div ng-show="waitingForColors">
  <div inkmonk-countdown  start="60" runduring="waitingForColors"></div> seconds left
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment