overtimer
Version:
Mission critical updateable javascript timer. It can handle overtimes also limits.
1 lines • 10.8 kB
JavaScript
"use strict";function Overtimer(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1e3,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null,r=null,a={duration:t,overtimeLimit:t,overtimeBump:t,poll:100,delay:0,repeat:1,debug:!1,start:!0};this.eventHandlers={start:[],tick:[],stop:[],pause:[],resume:[],delaystart:[],delayend:[],bump:[],repeat:[],finish:[],update:[],poll:[]},"object"===(void 0===e?"undefined":_typeof(e))?this.options=Object.assign({},a,e):("function"==typeof e&&(r=e),this.options=Object.assign({},a)),"function"==typeof i&&(r=i);var s=!1;"number"!=typeof t?(this.log("Duration must be number value.",1e3),s=!0):t<=0&&(this.log("Duration must be bigger than 0.",1001),s=!0),s&&(this.options.duration=1e3,this.options.overtimeLimit=1e3,this.options.overtimeBump=1e3),this.version="0.1.6",this.globalTimerId=null,this.state=Overtimer.STATES.CREATED,this.createdAt=Date.now(),this.startedAt=-1,this.delayStartedAt=-1,this.delayEndedAt=-1,this.repeatedAt=-1,this.tickedAt=-1,this.stoppedAt=-1,this.finishedAt=-1,this.pausedAt=-1,this.resumedAt=-1,this.bumpedAt=-1,this.pausedTime=-1,this.delayedTime=-1,this.overTime=-1,this.elapsedTime=-1,this.remainingTime=-1,this.totalDelayedTime=-1,this.totalElapsedTime=-1,this.totalRemainingTime=-1,this.currentRepeat=-1,this.repeatDuration=-1,this.repeatDurationWithDelay=-1,this.totalDuration=-1,this.totalDurationWithDelay=-1,this.currentRepeatPercentWithDelay=-1,this.currentRepeatPercent=-1,this.totalPercentWithDelay=-1,this.totalPercent=-1,this.timesUpdatedAt=-1,this.lastPollAt=-1,"function"==typeof r&&this.on("finish",r),this.options.start&&this.start()}var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};Overtimer.STATES={CREATED:0,WAITING:1,RUNNING:2,PAUSED:3,STOPPED:4},Overtimer.global={callbacks:[],timer:null,updateMs:1,lastId:0,join:function join(t){var e=Overtimer.global.callbacks.find(function(e){return e.callback===t});return void 0!==e?e.id:(Overtimer.global.lastId+=1,Overtimer.global.callbacks.push({callback:t,id:Overtimer.global.lastId}),null===Overtimer.global.timer&&(Overtimer.global.timer=setInterval(Overtimer.global.tick,Overtimer.global.updateMs)),Overtimer.global.lastId)},leave:function leave(t){Overtimer.global.callbacks=Overtimer.global.callbacks.filter(function(e){return e.id!==t}),0===Overtimer.global.callbacks.length&&null!==Overtimer.global.timer&&(clearInterval(Overtimer.global.timer),Overtimer.global.timer=null)},tick:function tick(){Overtimer.global.callbacks.forEach(function(t){t.callback()})}},Overtimer.prototype.log=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"Unexcepted error.",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1;this.options.debug&&console.log((-1!==e?"( "+e.toString()+" ): ":"")+" "+t)},Overtimer.prototype.on=function(t,e){return"string"!=typeof t?(this.log("Event name must be string.",1002),!1):t.length<1?(this.log("Event name length be bigger than 0.",1003),!1):void 0===this.eventHandlers[t]?(this.log("Event name not registered!",1004),!1):"function"!=typeof e?(this.log("Callback is not function!",1005),!1):(this.eventHandlers[t].push(e),!0)},Overtimer.prototype.off=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return"string"!=typeof t?(this.log("Event name must be string.",1006),!1):t.length<1?(this.log("Event name length be bigger than 0.",1007),!1):void 0===this.eventHandlers[t]?(this.log("Event name not registered!",1008),!1):(this.eventHandlers[t]="function"==typeof e?this.eventHandlers[t].filter(function(t){return t!==e}):[],!0)},Overtimer.prototype.trigger=function(t){for(var e=this,i=arguments.length,r=Array(i>1?i-1:0),a=1;a<i;a++)r[a-1]=arguments[a];return"string"!=typeof t?(this.log("Event name must be string.",1009),!1):t.length<1?(this.log("Event name's length must bigger than 1.",1010),!1):void 0!==this.eventHandlers[t.toLowerCase()]&&Array.isArray(this.eventHandlers[t.toLowerCase()])?(this.eventHandlers[t.toLowerCase()].forEach(function(t){t.call.apply(t,[e].concat(r))}),!0):(this.log("Event not found in list.",1011),!1)},Overtimer.prototype.joinToMainInterval=function(){this.globalTimerId=Overtimer.global.join(this.tickMainInterval.bind(this))},Overtimer.prototype.tickMainInterval=function(){var t=Date.now(),e=t-this.timesUpdatedAt;this.state===Overtimer.STATES.RUNNING?(this.elapsedTime+=e,this.remainingTime-=e,this.totalElapsedTime+=e,this.totalRemainingTime-=e,this.currentRepeatPercentWithDelay=parseFloat((this.elapsedTime+this.delayedTime)/this.repeatDurationWithDelay*100),this.totalPercentWithDelay=parseFloat((this.totalElapsedTime+this.totalDelayedTime)/this.totalDurationWithDelay*100),this.currentRepeatPercent=parseFloat(this.elapsedTime/this.repeatDuration*100),this.totalPercent=parseFloat(this.totalElapsedTime/this.totalDuration*100)):this.state===Overtimer.STATES.PAUSED?this.pausedTime+=e:this.state===Overtimer.STATES.WAITING&&(this.delayedTime+=e,this.totalDelayedTime+=e,this.currentRepeatPercentWithDelay=parseFloat((this.elapsedTime+this.delayedTime)/this.repeatDurationWithDelay*100),this.totalPercentWithDelay=parseFloat((this.totalElapsedTime+this.totalDelayedTime)/this.totalDurationWithDelay*100),this.delayedTime>=this.options.delay&&this.endDelay()),this.timesUpdatedAt=t,this.trigger("update"),this.lastPollAt+this.options.poll<Date.now()&&(this.trigger("poll"),this.lastPollAt=Date.now()),this.remainingTime<1&&this.tick()},Overtimer.prototype.leaveFromMainInterval=function(){Overtimer.global.leave(this.globalTimerId),this.globalTimerId=null},Overtimer.prototype.bump=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:-1;if(this.state===Overtimer.STATES.STOPPED||this.state===Overtimer.STATES.CREATED)return this.log("Can't use overtime bump on stopped or created state.",1011),!1;if("number"!=typeof this.options.overtimeLimit||this.options.overtimeLimit<=0)return this.log("Can't use overtime bump when overtime limit below 0.",1012),!1;var e="number"==typeof t&&t>0?t:this.options.overtimeBump;if("number"!=typeof e||e<=0)return this.log("Bump value is not valid! Must be number and bigger than 0 for bump:",1013),this.log(e,1014),!1;var i=this.options.overtimeLimit-this.remainingTime;return i<0?(this.log("Timer not reached the overtime limit yet.",1015),!1):(e<i?(this.remainingTime+=e,this.totalRemainingTime+=e,this.overTime+=e,this.repeatDuration+=e,this.repeatDurationWithDelay+=e,this.totalDuration+=e,this.totalDurationWithDelay+=e,this.bumpedAt=Date.now(),this.trigger("bump",e,this.remainingTime)):(this.remainingTime+=i,this.totalRemainingTime+=i,this.overTime+=i,this.repeatDuration+=i,this.repeatDurationWithDelay+=i,this.totalDuration+=i,this.totalDurationWithDelay+=i,this.bumpedAt=Date.now(),this.trigger("bump",i,this.remainingTime)),!0)},Overtimer.prototype.start=function(){if(this.state===Overtimer.STATES.RUNNING||this.state===Overtimer.STATES.WAITING)return this.log("Timer is already started.",1016),!1;var t=this.options.duration*this.options.repeat;return this.options.delay>0?(this.state=Overtimer.STATES.WAITING,this.delayedTime=0,this.totalDelayedTime=0,this.delayStartedAt=Date.now(),this.totalDurationWithDelay=t+this.options.repeat*this.options.delay,this.repeatDurationWithDelay=this.options.duration+this.options.delay):(this.state=Overtimer.STATES.RUNNING,this.totalDurationWithDelay=t,this.repeatDurationWithDelay=this.options.duration),this.startedAt=Date.now(),this.elapsedTime=0,this.totalElapsedTime=0,this.pausedTime=0,this.overTime=0,this.remainingTime=this.options.duration,this.repeatDuration=this.options.duration,this.totalRemainingTime=t,this.totalDuration=t,this.currentRepeat=1,this.currentRepeatPercentWithDelay=0,this.currentRepeatPercent=0,this.totalPercentWithDelay=0,this.totalPercent=0,this.timesUpdatedAt=Date.now(),this.lastPollAt=Date.now(),this.joinToMainInterval(),this.trigger("start"),this.options.delay>0&&this.trigger("delaystart"),!0},Overtimer.prototype.pause=function(){return this.state!==Overtimer.STATES.RUNNING&&this.state!==Overtimer.STATES.WAITING?(this.log("Can't pause when timer not running.",1017),!1):(this.state=Overtimer.STATES.PAUSED,this.pausedAt=Date.now(),!0)},Overtimer.prototype.endDelay=function(){if(this.state!==Overtimer.STATES.WAITING)return this.log("Can't end delay when timer not waiting.",1018),!1;if("number"!=typeof this.options.delay||this.options.delay<=0)return this.log("Can't end delay when delay option not number or delay below 0.",1019),!1;var t=this.options.delay-this.delayedTime;return this.delayEndedAt=Date.now(),this.state=Overtimer.STATES.RUNNING,t>0&&(this.totalDuration-=t,this.totalDurationWithDelay-=t,this.repeatDuration-=t,this.repeatDurationWithDelay-=t),this.trigger("delayend"),!0},Overtimer.prototype.resume=function(){return this.state!==Overtimer.STATES.PAUSED?(this.log("Can't resume when timer not paused.",1020),!1):(this.options.delay>0&&this.delayedTime<this.options.delay?this.state=Overtimer.STATES.WAITING:this.state=Overtimer.STATES.RUNNING,this.resumedAt=Date.now(),!0)},Overtimer.prototype.repeat=function(){return this.state!==Overtimer.STATES.RUNNING&&this.state!==Overtimer.STATES.WAITING?(this.log("Can't repeat when timer not running.",1021),!1):(this.totalDuration-=this.remainingTime,this.totalDurationWithDelay-=this.remainingTime,this.currentRepeat+=1,this.elapsedTime=0,this.remainingTime=this.options.duration,this.repeatedAt=Date.now(),this.totalRemainingTime=this.options.repeat*this.options.duration-(this.currentRepeat-1)*this.options.duration,this.currentRepeatPercentWithDelay=0,this.currentRepeatPercent=0,this.options.delay>0?(this.delayedTime=0,this.state=Overtimer.STATES.WAITING):this.state=Overtimer.STATES.RUNNING,this.timesUpdatedAt=Date.now(),this.trigger("repeat"),this.options.delay>0&&this.trigger("delaystart"),!0)},Overtimer.prototype.tick=function(){this.tickedAt=Date.now(),this.trigger("tick"),this.currentRepeat<this.options.repeat?this.repeat():(this.finishedAt=Date.now(),this.totalRemainingTime=0,this.remainingTime=0,this.stoppedAt=Date.now(),this.totalPercentWithDelay=100,this.totalPercent=100,this.currentRepeatPercent=100,this.currentRepeatPercentWithDelay=100,this.trigger("update"),this.trigger("poll"),this.trigger("finish"),this.stop())},Overtimer.prototype.stop=function(){return this.state===Overtimer.STATES.STOPPED?(this.log("Timer is already stopped.",1022),!1):(this.leaveFromMainInterval(),this.state=Overtimer.STATES.STOPPED,this.stoppedAt=Date.now(),this.trigger("stop"),!0)},"undefined"!=typeof module&&(module.exports=Overtimer),"undefined"!=typeof window&&(window.Overtimer=Overtimer);