Skip to content

Instantly share code, notes, and snippets.

@heluvaguy
Created November 9, 2013 10:43
Show Gist options
  • Save heluvaguy/7384190 to your computer and use it in GitHub Desktop.
Save heluvaguy/7384190 to your computer and use it in GitHub Desktop.
Create Clock Using AngularJs
var myApp = angular.module('myApp', []);
myApp.directive('clock', function($timeout, dateFilter){
return function(scope, element, attrs){
var timeoutId; // timeoutId, so that we can cancel the time updates
// schedule update in one second
function updateLater() {
// save the timeoutId for canceling
timeoutId = $timeout(function() {
element.text(dateFilter(new Date(), 'yyyy/MM/dd HH:mm:ss '));
updateLater(); // schedule another update
}, 1000);
}
// listen on DOM destroy (removal) event, and cancel the next UI update
// to prevent updating time ofter the DOM element was removed.
element.bind('$destroy', function() {
$timeout.cancel(timeoutId);
});
updateLater(); // kick off the UI update process.
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment