function Timer(intervalValue){ this.intervalValue = intervalValue ? intervalValue : 500; this.interval = null; this.events = []; this.listeners = []; this.onstart = new DOMEvent(); this.onstop = new DOMEvent(); this.isTicking = false; } Timer.prototype.start = function(){ if(this.isTicking){ return; } this.calls = 0; this.timeElapsed = 0; this.interval = setInterval( this.intervalFunction.bind(this), this.intervalValue ); this.isTicking = true; this.onstart.fire(); } Timer.prototype.intervalFunction = function(){ this.calls ++; this.timeElapsed += this.intervalValue; this.events.each(function(ev, index){ if(this.calls % ev.freq == 0){ var ret = ev.func.apply(ev.oThis, ev.args); if(typeof ret != 'undefined' && ret !== false){ this.listeners.each(function(lnr, index){ if(lnr.name == ev.name){ lnr.func.apply(lnr.oThis, [ret]) } }) } } }.bind(this)) } Timer.prototype.registerEvent = function(obj){ if(!obj.freq){ obj.freq = 1; } this.events.push(obj); } Timer.prototype.registerEventListener = function(obj){ this.listeners.push(obj); } Timer.prototype.clear = function(){ clearInterval(this.interval); this.isTicking = false; this.onstop.fire(); } Timer.prototype.toggle = function(){ if(this.isTicking){ this.clear(); }else{ this.start(); } } Timer.prototype.restart = function(){ this.clear(); this.start(); } Timer.prototype.toString = __toString; Timer.prototype.__name = "Timer"; Timer.detectTimer = function(oTimer){ if(!oTimer){ return new Timer(50); }else if(oTimer instanceof Number){ return new Timer(oTimer) }else if(oTimer instanceof Timer){ oTimer._MUTUAL = true; return oTimer; } }