123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- /**
- * @package jQuery Timer
- * @desc this plugin for jQuery of Timer as based from jQuery ui plugin frame
- * @date 2013-5-10
- * @version 1.0.1
- * @author viticm
- */
- ( function( $, undefined )
- {
- $.timer = $.timer || {};
- $.timer.console = $.timer.console || {}; // debug
- if( 'undefined' == typeof console ) // if not found client object then use default output function
- {
- $.extend( $.timer.console,
- {
- error: function( cErrorStr )
- {
- alert( '[ERROR] '+cErrorStr );
- return;
- },
- warn: function( cWarningStr )
- {
- alert( '[WARNING] '+cWarningStr );
- },
- log: function( cLogStr )
- {
- alert( '[LOG] '+cLogStr );
- }
- });
- }
- else
- {
- $.timer.console = console; // notice: this( console ) object can't extend
- }
- $.extend( $.timer,
- {
- version: '1.0.1',
- options:
- {
- iTimerDelay: 1000,
- iRepeatCount: 10,
- iCurrentCount: 0,
- bRunning: false,
- cRepeatType: null == this.iRePeatCount || 1 > this.iRepeatCount ? 'interval' : 'timeout',
- bCompleted: false,
- timerEventRun: { bBubbles: false, bCancleable: false },
- timerEventComplete: { bBubbles: false, bCancleable: false },
- funcListener: null,
- bDebug: false,
- name: '',
- OBJ_StartDate: null,
- userData: {}
- },
- iTimerId: 0,
- OBJ_TimerEventRun: null,
- OBJ_TimerEventComplete: null,
- handler: {}
- });
- $.extend( $.timer,
- {
- init: function( options )
- {
- $.extend( this.options, this.options, options || {} );
- this.OBJ_TimerEventRun = this.timerEvent.init( this.timerEventRun );
- this.OBJ_TimerEventComplete = this.timerEvent.init( this.timerEventComplete );
- var Arr_ListenerMap = []; //hanler listener map
- Arr_ListenerMap[ this.timerEvent.TIMER ] = [];
- Arr_ListenerMap[ this.timerEvent.TIMER_COMPLETE ] = [];
- this.handler = Arr_ListenerMap;
- return $.extend( true, {}, this );
- }
-
- });
-
- $.extend( $.timer,
- {
- timerEvent:
- {
- cType: 'timer',
- bBubbles: false,
- bCancleable: false,
- init: function( timerEventSet )
- {
- var timerEventSet = 'undefined' === typeof timerEventSet ? {} : timerEventSet;
- this.cType = undefined === timerEventSet.cType ? this.cType : timerEventSet.cType;
- this.bBubbles = timerEventSet || undefined === timerEventSet.bBubbles ? this.bBubbles : timerEventSet.bBubbles;
- this.bCancleable = undefined === timerEventSet.bCancleable ? this.bCancleable : timerEventSet.bCancleable;
- return this;
- },
- TIMER: 'timer',
- TIMER_COMPLETE: 'timerComplete',
- toString: function()
- {
- return '[timerEvent type='+this.cType
- +' bubbles='+this.bBubbles
- +' cancleable='+this.bCancleable
- +']';
- }
- }
- });
-
- // listeners
- $.extend( $.timer,
- {
- addEventListener: function( cType, funcListener, bUseCapture )
- {
- if( this.timerEvent.TIMER == cType || this.timerEvent.TIMER_COMPLETE == cType )
- {
- if( !funcListener && true === this.options.bDebug )
- $.timer.console.warn( 'not found listener function! timer name: ' + this.options.name );
- if( true === bUseCapture )
- {
- this.handler[ cType ].splice( 0, 0, [ funcListener ] );
- }
- else
- {
- this.handler[ cType ].push( funcListener );
- }
- }
- },
- removeEventListener: function( cType, funcListener )
- {
- if( this.timerEvent.TIMER === cType || this.timerEvent.TIMER_COMPLETE === cType )
- {
- if( !funcListener )
- {
- this.handler[ cType ] = [];
- }
- else
- {
- var Arr_TypeListener = this.handler[ cType ];
- for( var index = 0; index < Arr_TypeListener; index++ )
- {
- if( funcListener === Arr_TypeListener[ index ] )
- {
- Arr_TypeListener.splice( index, 1 );
- break;
- }
- }
- }
- }
- },
- // delay function for time out
- delayExecute: function( Arr_Listener )
- {
- var OBJ_TimerThis = this;
- this.dispatchListener( Arr_Listener, this.OBJ_TimerEventRun );
- this.options.iCurrentCount++;
- if( this.options.iCurrentCount < this.options.iRepeatCount )
- {
- if( true === this.options.bRunning )
- {
- this.iTimerId = setTimeout( function()
- {
- OBJ_TimerThis.delayExecute( Arr_Listener );
- }, this.options.iTimerDelay );
- }
- }
- else
- {
- this.options.bRunning = false;
- }
- if( false === this.options.bRunning )
- {
- if( false === this.options.bCompleted )
- {
- this.dispatchListener( this.handler[ this.timerEvent.TIMER_COMPLETE ],
- this.OBJ_TimerEventComplete );
- this.options.bCompleted = true;
- }
- }
- },
- // normal do listener function
- dispatchListener: function( Arr_Listener, OBJ_TimerEvent )
- {
- for( var prop in Arr_Listener )
- {
- Arr_Listener[ prop ]( OBJ_TimerEvent );
- }
- }
- });
-
- // actions
- $.extend( $.timer,
- {
- start: function()
- {
- var OBJ_TimerThis = this;
- if( true === this.options.bRunning || true === this.options.bCompleted )
- return;
- if( 0 === this.handler[ this.timerEvent.TIMER ].length
- && 0 === this.handler[ this.timerEvent.TIMER_COMPLETE ].length )
- {
- this.console.warn( 'not found listener function! timer name: ' + this.options.name );
- return;
- }
- this.options.bRunning = true;
- this.options.OBJ_StartDate = new Date();
- if( 'timeout' == this.options.cRepeatType )
- {
- this.iTimerId = setTimeout( function()
- {
- OBJ_TimerThis.delayExecute( OBJ_TimerThis.handler[ OBJ_TimerThis.timerEvent.TIMER ],
- OBJ_TimerThis.OBJ_TimerEventRun );
-
- }, this.options.iTimerDelay );
- }
- else
- {
- this.iTimerId = setInterval( function()
- {
- OBJ_TimerThis.dispatchListener( OBJ_TimerThis.handler[ OBJ_TimerThis.timerEvent.TIMER ],
- OBJ_TimerThis.OBJ_TimerEventRun );
- }, this.options.iTimerDelay );
- }
-
- },
- stop: function()
- {
- if( null === this.iTimerId ) return;
- if( 'timeout' == this.options.cRepeatType )
- {
- clearTimeout( this.iTimerId );
- this.options.bRunning = false;
- }
- else
- {
- clearInterval( this.iTimerId );
- }
- if( false === this.options.bCompleted )
- {
- this.dispatchListener( this.handler[ this.timerEvent.TIMER_COMPLETE ],
- this.OBJ_TimerEventComplete );
- }
- this.options.bCompleted = true;
- },
- reset: function()
- {
- this.options.iCurrentCount = 0;
- this.options.bRunning = true;
- this.options.bCompleted = false;
- this.options.OBJ_StartTime = new Date();
- }
- });
-
- // extend functions
- $.extend( $.timer,
- {
- getRunTime: function()
- {
- var OBJ_NowDate = new Date();
- if( !this.options.OBJ_StartDate ) return false;
- return OBJ_NowDate.getTime() - this.options.OBJ_StartDate.getTime();
- }
- });
- })( jQuery );
|