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(){ 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){ var ret = ev.func.apply(ev.oThis, ev.args); if(typeof ret != 'undefined' && ret === true){ this.listeners.each(function(lnr, index){ if(lnr.name == ev.name){ lnr.func.apply(lnr.oThis, [ret]) } }) } }.bind(this)) } Timer.prototype.registerEvent = function(obj){ 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(); }