Last active
August 29, 2015 14:22
-
-
Save iotaweb/14758ba6d30a741d13d8 to your computer and use it in GitHub Desktop.
Angular.js countdown timer directive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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