diff --git a/cocos2d/core/CCScheduler.js b/cocos2d/core/CCScheduler.js index 0c75dc15af..d83805741b 100644 --- a/cocos2d/core/CCScheduler.js +++ b/cocos2d/core/CCScheduler.js @@ -193,16 +193,9 @@ cc.inject({ this._useDelay = (this._delay > 0); this._repeat = repeat; this._runForever = (this._repeat === cc.REPEAT_FOREVER); + this._timesExecuted = 0; return true; }, - /** - * @return {Number} returns interval of timer - */ - getInterval : function(){return this._interval;}, - /** - * @param {Number} interval set interval in seconds - */ - setInterval : function(interval){this._interval = interval;}, /** * triggers the timer @@ -222,22 +215,20 @@ cc.inject({ } else {//advanced usage if (this._useDelay) { if (this._elapsed >= this._delay) { + this._timesExecuted += 1; // important to increment before call trigger this.trigger(); - this._elapsed -= this._delay; - this._timesExecuted += 1; this._useDelay = false; } } else { if (this._elapsed >= this._interval) { + this._timesExecuted += 1; // important to increment before call trigger this.trigger(); - this._elapsed = 0; - this._timesExecuted += 1; } } - if (this._callback && !this._runForever && this._timesExecuted > this._repeat) + if (this._callback && this.isExhausted()) this.cancel(); } } @@ -260,6 +251,10 @@ cc.inject({ cancel: function () { //override this._scheduler.unschedule(this._callback, this._target); + }, + + isExhausted: function () { + return !this._runForever && this._timesExecuted > this._repeat; } }, CallbackTimer.prototype); @@ -609,9 +604,9 @@ cc.Scheduler = cc.Class.extend(/** @lends cc.Scheduler# */{ } else { for (i = 0; i < element.timers.length; i++) { timer = element.timers[i]; - if (callback === timer._callback) { + if (!timer.isExhausted() && callback === timer._callback) { cc.log(cc._LogInfos.Scheduler_scheduleCallbackForTarget, timer.getInterval().toFixed(4), interval.toFixed(4)); - timer._interval = interval; + timer.initWithCallback(this, callback, target, interval, repeat, delay, key); return; } }