12 |
- /** jQuery timer, by viticm( viticm@126.com ) **/
- !function(a,b){a.timer=a.timer||{},a.timer.console=a.timer.console||{},"undefined"==typeof console?a.extend(a.timer.console,{error:function(a){alert("[ERROR] "+a)},warn:function(a){alert("[WARNING] "+a)},log:function(a){alert("[LOG] "+a)}}):a.timer.console=console,a.extend(a.timer,{version:"1.0.1",options:{iTimerDelay:1e3,iRepeatCount:10,iCurrentCount:0,bRunning:!1,cRepeatType:null==this.iRePeatCount||1>this.iRepeatCount?"interval":"timeout",bCompleted:!1,timerEventRun:{bBubbles:!1,bCancleable:!1},timerEventComplete:{bBubbles:!1,bCancleable:!1},funcListener:null,bDebug:!1,name:"",OBJ_StartDate:null,userData:{}},iTimerId:0,OBJ_TimerEventRun:null,OBJ_TimerEventComplete:null,handler:{}}),a.extend(a.timer,{init:function(b){a.extend(this.options,this.options,b||{}),this.OBJ_TimerEventRun=this.timerEvent.init(this.timerEventRun),this.OBJ_TimerEventComplete=this.timerEvent.init(this.timerEventComplete);var c=[];return c[this.timerEvent.TIMER]=[],c[this.timerEvent.TIMER_COMPLETE]=[],this.handler=c,a.extend(!0,{},this)}}),a.extend(a.timer,{timerEvent:{cType:"timer",bBubbles:!1,bCancleable:!1,init:function(a){var a="undefined"==typeof a?{}:a;return this.cType=b===a.cType?this.cType:a.cType,this.bBubbles=a||b===a.bBubbles?this.bBubbles:a.bBubbles,this.bCancleable=b===a.bCancleable?this.bCancleable:a.bCancleable,this},TIMER:"timer",TIMER_COMPLETE:"timerComplete",toString:function(){return"[timerEvent type="+this.cType+" bubbles="+this.bBubbles+" cancleable="+this.bCancleable+"]"}}}),a.extend(a.timer,{addEventListener:function(b,c,d){(this.timerEvent.TIMER==b||this.timerEvent.TIMER_COMPLETE==b)&&(c||!0!==this.options.bDebug||a.timer.console.warn("not found listener function! timer name: "+this.options.name),!0===d?this.handler[b].splice(0,0,[c]):this.handler[b].push(c))},removeEventListener:function(a,b){var c,d;if(this.timerEvent.TIMER===a||this.timerEvent.TIMER_COMPLETE===a)if(b){for(c=this.handler[a],d=0;c>d;d++)if(b===c[d]){c.splice(d,1);break}}else this.handler[a]=[]},delayExecute:function(a){var b=this;this.dispatchListener(a,this.OBJ_TimerEventRun),this.options.iCurrentCount++,this.options.iCurrentCount<this.options.iRepeatCount?!0===this.options.bRunning&&(this.iTimerId=setTimeout(function(){b.delayExecute(a)},this.options.iTimerDelay)):this.options.bRunning=!1,!1===this.options.bRunning&&!1===this.options.bCompleted&&(this.dispatchListener(this.handler[this.timerEvent.TIMER_COMPLETE],this.OBJ_TimerEventComplete),this.options.bCompleted=!0)},dispatchListener:function(a,b){for(var c in a)a[c](b)}}),a.extend(a.timer,{start:function(){var a=this;if(!0!==this.options.bRunning&&!0!==this.options.bCompleted){if(0===this.handler[this.timerEvent.TIMER].length&&0===this.handler[this.timerEvent.TIMER_COMPLETE].length)return this.console.warn("not found listener function! timer name: "+this.options.name),void 0;this.options.bRunning=!0,this.options.OBJ_StartDate=new Date,this.iTimerId="timeout"==this.options.cRepeatType?setTimeout(function(){a.delayExecute(a.handler[a.timerEvent.TIMER],a.OBJ_TimerEventRun)},this.options.iTimerDelay):setInterval(function(){a.dispatchListener(a.handler[a.timerEvent.TIMER],a.OBJ_TimerEventRun)},this.options.iTimerDelay)}},stop:function(){null!==this.iTimerId&&("timeout"==this.options.cRepeatType?(clearTimeout(this.iTimerId),this.options.bRunning=!1):clearInterval(this.iTimerId),!1===this.options.bCompleted&&this.dispatchListener(this.handler[this.timerEvent.TIMER_COMPLETE],this.OBJ_TimerEventComplete),this.options.bCompleted=!0)},reset:function(){this.options.iCurrentCount=0,this.options.bRunning=!0,this.options.bCompleted=!1,this.options.OBJ_StartTime=new Date}}),a.extend(a.timer,{getRunTime:function(){var a=new Date;return this.options.OBJ_StartDate?a.getTime()-this.options.OBJ_StartDate.getTime():!1}})}(jQuery);
|