Skip to content

Instantly share code, notes, and snippets.

@Insayt
Created November 17, 2015 11:48
Show Gist options
  • Save Insayt/f97fcd220efb0c7534ce to your computer and use it in GitHub Desktop.
Save Insayt/f97fcd220efb0c7534ce to your computer and use it in GitHub Desktop.
Derictive
app.directive('counter', function() {
var interval,
count = 0;
return {
link: function($scope, element, attrs, $rootScope) {
$scope.$on('counter:start', function() {
interval = setInterval(function() {
count += 1000;
$(element).find('#counter').html(msToTime(count));
}, 1000);
});
$scope.$on('counter:stop', function() {
clearInterval(interval);
$(element).find('#counter').html(msToTime(count));
});
function msToTime(duration) {
var seconds = parseInt((duration/1000)%60)
, minutes = parseInt((duration/(1000*60))%60)
, hours = parseInt((duration/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment