Skip to content

Instantly share code, notes, and snippets.

@iotaweb
Last active August 29, 2015 14:22
Show Gist options
  • Save iotaweb/14758ba6d30a741d13d8 to your computer and use it in GitHub Desktop.
Save iotaweb/14758ba6d30a741d13d8 to your computer and use it in GitHub Desktop.
Angular.js countdown timer directive
/*
* AngularJS Countdown Timer Directive
* Author: Rob Anderson @iotaweb
* Dependencies: https://github.com/EvanHahn/HumanizeDuration.js
* Example: <span countdown class="pull-right" prefix="Token expires in" expires="{{ token.expiresAt }}"></span>
*/
(function () {
'use strict';
angular
.module('app')
.directive('countdown', countdown);
function countdown ($interval, auth) {
return {
restrict: 'A',
scope: {
prefix: '@',
expires: '@'
},
link: function (scope, element) {
var expiresAt = parseInt(scope.expires, 10);
var prefix = '';
if (scope.prefix) {
prefix = scope.prefix + ' ';
}
scope.$watch('expires', function () {
expiresAt = parseInt(scope.expires, 10);
});
$interval(function () {
var remaining = expiresAt - Math.floor(Date.now() / 1000);
return element.text(prefix + humanizeDuration(remaining * 1000, {
halfUnit: false
}));
}, 1000);
}
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment