我已經解決了很多個小時,但我不知道爲什麼我不能解決它。我有以下代碼:Javascript:爲什麼我無法在我的對象行爲中清除超時?
var Timer_Tick;
var Game_Engine = {
//Set the min and sec of the timer by default is 2 min
min:1,
sec:60,
star_count: 0,
tongue_count: 0,
time: '',
star1: '',
star2: '',
star3: '',
star_bar: '',
tongue_bar: '',
frog: '',
bug: '',
flower: '',
Timer: function() {
//Start Timer
var that = this;
var stop = function() { that.stopTimer(); };
Timer_Tick = setTimeout(function() {
var Time = new Date(0, 0, 0, 0, that.min, that.sec--).toTimeString().slice(3,8);
that.time.text = Time;
console.log(that.min + ':' + that.sec);
if (that.min === 1 && that.sec === 40) {
that.bug.gotoAndPlay('take_photo');
that.flower.onPress = null;
stop();
}
else if (that.min === 1 && that.sec === -60) {
that.stopTimer();
//clearTimeout(Timer_Tick);
that.time.text = '00:00';
createjs.Tween.get(that.bug, { loop: false }).to({ y: that.bug.y + 100 }, 400, createjs.Ease.linear);
setTimeout(function() {
that.bug.gotoAndPlay('bite');
setTimeout(function() {
that.flower.gotoAndPlay('broken_flower');
that.frog.gotoAndPlay('lose');
}, 200);
createjs.Tween.get(that.bug, { loop: false }).wait(500).to({ x: 200, y: -100 }, 6000, createjs.Ease.linear);
}, 400);
}
that.Timer();
}, 100);
},
stopTimer: function() {
//Pause or stop Timer
clearTimeout(Timer_Tick);
}
};
我已經把debugger
找出我的事件被觸發與否。其結果是正如預期的那樣被正確觸發,但是Timer並沒有停下來,並且一直持續下去。任何人都可以幫助我,我現在真的需要你的幫助。我是新來的JavaScript :)。提前致謝。
+1,用於正確縮進代碼。 – 2013-02-15 07:39:09
我可以看到的唯一問題是'Timer_Tick'中的值。在設置定時器時提醒該值,並在'stopTimer'中再次看到您正在設置並獲取正確的值。 – 2013-02-15 07:44:20
我喜歡縮進我的代碼,以便我可以輕鬆閱讀它:) – 2013-02-15 08:26:18