jquery.timer.dev.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * @package jQuery Timer
  3. * @desc this plugin for jQuery of Timer as based from jQuery ui plugin frame
  4. * @date 2013-5-10
  5. * @version 1.0.1
  6. * @author viticm
  7. */
  8. ( function( $, undefined )
  9. {
  10. $.timer = $.timer || {};
  11. $.timer.console = $.timer.console || {}; // debug
  12. if( 'undefined' == typeof console ) // if not found client object then use default output function
  13. {
  14. $.extend( $.timer.console,
  15. {
  16. error: function( cErrorStr )
  17. {
  18. alert( '[ERROR] '+cErrorStr );
  19. return;
  20. },
  21. warn: function( cWarningStr )
  22. {
  23. alert( '[WARNING] '+cWarningStr );
  24. },
  25. log: function( cLogStr )
  26. {
  27. alert( '[LOG] '+cLogStr );
  28. }
  29. });
  30. }
  31. else
  32. {
  33. $.timer.console = console; // notice: this( console ) object can't extend
  34. }
  35. $.extend( $.timer,
  36. {
  37. version: '1.0.1',
  38. options:
  39. {
  40. iTimerDelay: 1000,
  41. iRepeatCount: 10,
  42. iCurrentCount: 0,
  43. bRunning: false,
  44. cRepeatType: null == this.iRePeatCount || 1 > this.iRepeatCount ? 'interval' : 'timeout',
  45. bCompleted: false,
  46. timerEventRun: { bBubbles: false, bCancleable: false },
  47. timerEventComplete: { bBubbles: false, bCancleable: false },
  48. funcListener: null,
  49. bDebug: false,
  50. name: '',
  51. OBJ_StartDate: null,
  52. userData: {}
  53. },
  54. iTimerId: 0,
  55. OBJ_TimerEventRun: null,
  56. OBJ_TimerEventComplete: null,
  57. handler: {}
  58. });
  59. $.extend( $.timer,
  60. {
  61. init: function( options )
  62. {
  63. $.extend( this.options, this.options, options || {} );
  64. this.OBJ_TimerEventRun = this.timerEvent.init( this.timerEventRun );
  65. this.OBJ_TimerEventComplete = this.timerEvent.init( this.timerEventComplete );
  66. var Arr_ListenerMap = []; //hanler listener map
  67. Arr_ListenerMap[ this.timerEvent.TIMER ] = [];
  68. Arr_ListenerMap[ this.timerEvent.TIMER_COMPLETE ] = [];
  69. this.handler = Arr_ListenerMap;
  70. return $.extend( true, {}, this );
  71. }
  72. });
  73. $.extend( $.timer,
  74. {
  75. timerEvent:
  76. {
  77. cType: 'timer',
  78. bBubbles: false,
  79. bCancleable: false,
  80. init: function( timerEventSet )
  81. {
  82. var timerEventSet = 'undefined' === typeof timerEventSet ? {} : timerEventSet;
  83. this.cType = undefined === timerEventSet.cType ? this.cType : timerEventSet.cType;
  84. this.bBubbles = timerEventSet || undefined === timerEventSet.bBubbles ? this.bBubbles : timerEventSet.bBubbles;
  85. this.bCancleable = undefined === timerEventSet.bCancleable ? this.bCancleable : timerEventSet.bCancleable;
  86. return this;
  87. },
  88. TIMER: 'timer',
  89. TIMER_COMPLETE: 'timerComplete',
  90. toString: function()
  91. {
  92. return '[timerEvent type='+this.cType
  93. +' bubbles='+this.bBubbles
  94. +' cancleable='+this.bCancleable
  95. +']';
  96. }
  97. }
  98. });
  99. // listeners
  100. $.extend( $.timer,
  101. {
  102. addEventListener: function( cType, funcListener, bUseCapture )
  103. {
  104. if( this.timerEvent.TIMER == cType || this.timerEvent.TIMER_COMPLETE == cType )
  105. {
  106. if( !funcListener && true === this.options.bDebug )
  107. $.timer.console.warn( 'not found listener function! timer name: ' + this.options.name );
  108. if( true === bUseCapture )
  109. {
  110. this.handler[ cType ].splice( 0, 0, [ funcListener ] );
  111. }
  112. else
  113. {
  114. this.handler[ cType ].push( funcListener );
  115. }
  116. }
  117. },
  118. removeEventListener: function( cType, funcListener )
  119. {
  120. if( this.timerEvent.TIMER === cType || this.timerEvent.TIMER_COMPLETE === cType )
  121. {
  122. if( !funcListener )
  123. {
  124. this.handler[ cType ] = [];
  125. }
  126. else
  127. {
  128. var Arr_TypeListener = this.handler[ cType ];
  129. for( var index = 0; index < Arr_TypeListener; index++ )
  130. {
  131. if( funcListener === Arr_TypeListener[ index ] )
  132. {
  133. Arr_TypeListener.splice( index, 1 );
  134. break;
  135. }
  136. }
  137. }
  138. }
  139. },
  140. // delay function for time out
  141. delayExecute: function( Arr_Listener )
  142. {
  143. var OBJ_TimerThis = this;
  144. this.dispatchListener( Arr_Listener, this.OBJ_TimerEventRun );
  145. this.options.iCurrentCount++;
  146. if( this.options.iCurrentCount < this.options.iRepeatCount )
  147. {
  148. if( true === this.options.bRunning )
  149. {
  150. this.iTimerId = setTimeout( function()
  151. {
  152. OBJ_TimerThis.delayExecute( Arr_Listener );
  153. }, this.options.iTimerDelay );
  154. }
  155. }
  156. else
  157. {
  158. this.options.bRunning = false;
  159. }
  160. if( false === this.options.bRunning )
  161. {
  162. if( false === this.options.bCompleted )
  163. {
  164. this.dispatchListener( this.handler[ this.timerEvent.TIMER_COMPLETE ],
  165. this.OBJ_TimerEventComplete );
  166. this.options.bCompleted = true;
  167. }
  168. }
  169. },
  170. // normal do listener function
  171. dispatchListener: function( Arr_Listener, OBJ_TimerEvent )
  172. {
  173. for( var prop in Arr_Listener )
  174. {
  175. Arr_Listener[ prop ]( OBJ_TimerEvent );
  176. }
  177. }
  178. });
  179. // actions
  180. $.extend( $.timer,
  181. {
  182. start: function()
  183. {
  184. var OBJ_TimerThis = this;
  185. if( true === this.options.bRunning || true === this.options.bCompleted )
  186. return;
  187. if( 0 === this.handler[ this.timerEvent.TIMER ].length
  188. && 0 === this.handler[ this.timerEvent.TIMER_COMPLETE ].length )
  189. {
  190. this.console.warn( 'not found listener function! timer name: ' + this.options.name );
  191. return;
  192. }
  193. this.options.bRunning = true;
  194. this.options.OBJ_StartDate = new Date();
  195. if( 'timeout' == this.options.cRepeatType )
  196. {
  197. this.iTimerId = setTimeout( function()
  198. {
  199. OBJ_TimerThis.delayExecute( OBJ_TimerThis.handler[ OBJ_TimerThis.timerEvent.TIMER ],
  200. OBJ_TimerThis.OBJ_TimerEventRun );
  201. }, this.options.iTimerDelay );
  202. }
  203. else
  204. {
  205. this.iTimerId = setInterval( function()
  206. {
  207. OBJ_TimerThis.dispatchListener( OBJ_TimerThis.handler[ OBJ_TimerThis.timerEvent.TIMER ],
  208. OBJ_TimerThis.OBJ_TimerEventRun );
  209. }, this.options.iTimerDelay );
  210. }
  211. },
  212. stop: function()
  213. {
  214. if( null === this.iTimerId ) return;
  215. if( 'timeout' == this.options.cRepeatType )
  216. {
  217. clearTimeout( this.iTimerId );
  218. this.options.bRunning = false;
  219. }
  220. else
  221. {
  222. clearInterval( this.iTimerId );
  223. }
  224. if( false === this.options.bCompleted )
  225. {
  226. this.dispatchListener( this.handler[ this.timerEvent.TIMER_COMPLETE ],
  227. this.OBJ_TimerEventComplete );
  228. }
  229. this.options.bCompleted = true;
  230. },
  231. reset: function()
  232. {
  233. this.options.iCurrentCount = 0;
  234. this.options.bRunning = true;
  235. this.options.bCompleted = false;
  236. this.options.OBJ_StartTime = new Date();
  237. }
  238. });
  239. // extend functions
  240. $.extend( $.timer,
  241. {
  242. getRunTime: function()
  243. {
  244. var OBJ_NowDate = new Date();
  245. if( !this.options.OBJ_StartDate ) return false;
  246. return OBJ_NowDate.getTime() - this.options.OBJ_StartDate.getTime();
  247. }
  248. });
  249. })( jQuery );