Created
November 17, 2015 11:48
-
-
Save Insayt/f97fcd220efb0c7534ce to your computer and use it in GitHub Desktop.
Derictive
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
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