1- /**
2- * angular-timer - v1.0.0 - 2013-07-22
3- * https://github.com/siddii/angular-timer
4- *
5- * Copyright (c) 2013 Siddique Hameed
6- * Licensed MIT <https://github.com/siddii/angular-timer>
7- */
8- angular . module ( "timer" , [ ] ) . directive ( "timer" , [ "$timeout" , "$compile" , function ( a , b ) { return { restrict :"E" , replace :! 1 , scope :{ interval :"=interval" , startTimeAttr :"=startTime" , countdownattr :"=countdown" } , controller :[ "$scope" , "$element" , function ( a , c ) { function d ( ) { a . timeoutId && clearTimeout ( a . timeoutId ) } 0 === c . html ( ) . trim ( ) . length && c . append ( b ( "<span>{{millis}}</span>" ) ( a ) ) , a . startTime = null , a . timeoutId = null , a . countdown = a . countdownattr && parseInt ( a . countdownattr , 10 ) > 0 ?parseInt ( a . countdownattr , 10 ) :void 0 , a . isRunning = ! 1 , a . $on ( "timer-start" , function ( ) { a . start ( ) } ) , a . $on ( "timer-resume" , function ( ) { a . resume ( ) } ) , a . $on ( "timer-stop" , function ( ) { a . stop ( ) } ) , a . start = c [ 0 ] . start = function ( ) { a . startTime = a . startTimeAttr ?new Date ( a . startTimeAttr ) :new Date , d ( ) , e ( ) } , a . resume = c [ 0 ] . resume = function ( ) { d ( ) , a . startTime = new Date - ( a . stoppedTime - a . startTime ) , e ( ) } , a . stop = c [ 0 ] . stop = function ( ) { a . stoppedTime = new Date , d ( ) , a . timeoutId = null } , c . bind ( "$destroy" , function ( ) { d ( ) } ) ; var e = function ( ) { a . countdown > 0 ?a . countdown -- :a . countdown <= 0 && a . stop ( ) , a . millis = new Date - a . startTime , a . countdown > 0 && ( a . millis = 1e3 * a . countdown ) , a . seconds = Math . floor ( a . millis / 1e3 % 60 ) , a . minutes = Math . floor ( a . millis / 6e4 % 60 ) , a . hours = Math . floor ( a . millis / 36e5 % 24 ) , a . days = Math . floor ( a . millis / 36e5 / 24 ) , a . timeoutId = setTimeout ( function ( ) { e ( ) , a . $apply ( ) } , a . interval ) , a . $emit ( "timer-tick" , { timeoutId :a . timeoutId , millis :a . millis } ) } ; a . start ( ) } ] } } ] ) ;
1+ angular . module ( 'timer' , [ ] )
2+ . directive ( 'timer' , function ( $timeout , $compile ) {
3+ return {
4+ restrict : 'E' ,
5+ replace : false ,
6+ scope : {
7+ interval : '=interval' ,
8+ startTimeAttr : '=startTime' ,
9+ countdownattr : '=countdown'
10+ } ,
11+ controller : function ( $scope , $element ) {
12+ if ( $element . html ( ) . trim ( ) . length === 0 ) {
13+ $element . append ( $compile ( '<span>{{millis}}</span>' ) ( $scope ) ) ;
14+ }
15+
16+ $scope . startTime = null ;
17+ $scope . timeoutId = null ;
18+ $scope . countdown = $scope . countdownattr && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
19+ $scope . isRunning = false ;
20+
21+ $scope . $on ( 'timer-start' , function ( ) {
22+ $scope . start ( ) ;
23+ } ) ;
24+
25+ $scope . $on ( 'timer-resume' , function ( ) {
26+ $scope . resume ( ) ;
27+ } ) ;
28+
29+ $scope . $on ( 'timer-stop' , function ( ) {
30+ $scope . stop ( ) ;
31+ } ) ;
32+
33+ function resetTimeout ( ) {
34+ if ( $scope . timeoutId ) {
35+ clearTimeout ( $scope . timeoutId ) ;
36+ }
37+ }
38+
39+ $scope . start = $element [ 0 ] . start = function ( ) {
40+ $scope . startTime = $scope . startTimeAttr ? new Date ( $scope . startTimeAttr ) : new Date ( ) ;
41+ resetTimeout ( ) ;
42+ tick ( ) ;
43+ } ;
44+
45+ $scope . resume = $element [ 0 ] . resume = function ( ) {
46+ resetTimeout ( ) ;
47+ $scope . startTime = new Date ( ) - ( $scope . stoppedTime - $scope . startTime ) ;
48+ tick ( ) ;
49+ } ;
50+
51+ $scope . stop = $element [ 0 ] . stop = function ( ) {
52+ $scope . stoppedTime = new Date ( ) ;
53+ resetTimeout ( ) ;
54+ $scope . timeoutId = null ;
55+ } ;
56+
57+ $element . bind ( '$destroy' , function ( ) {
58+ resetTimeout ( ) ;
59+ } ) ;
60+
61+ var tick = function ( ) {
62+ if ( $scope . countdown > 0 ) {
63+ $scope . countdown -- ;
64+ }
65+ else if ( $scope . countdown <= 0 ) {
66+ $scope . stop ( ) ;
67+ }
68+
69+ $scope . millis = new Date ( ) - $scope . startTime ;
70+
71+ if ( $scope . countdown > 0 ) {
72+ $scope . millis = $scope . countdown * 1000 ;
73+ }
74+
75+ $scope . seconds = Math . floor ( ( $scope . millis / 1000 ) % 60 ) ;
76+ $scope . minutes = Math . floor ( ( ( $scope . millis / ( 1000 * 60 ) ) % 60 ) ) ;
77+ $scope . hours = Math . floor ( ( ( $scope . millis / ( 1000 * 60 * 60 ) ) % 24 ) ) ;
78+ $scope . days = Math . floor ( ( ( $scope . millis / ( 1000 * 60 * 60 ) ) / 24 ) ) ;
79+ //We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
80+ $scope . timeoutId = setTimeout ( function ( ) {
81+ tick ( ) ;
82+ $scope . $apply ( ) ;
83+ } , $scope . interval ) ;
84+
85+ $scope . $emit ( 'timer-tick' , { timeoutId : $scope . timeoutId , millis : $scope . millis } ) ;
86+ } ;
87+
88+ $scope . start ( ) ;
89+ }
90+ } ;
91+ } ) ;
0 commit comments