Javascript:为什么我不能在我的对象行为中clearTimeout?

时间:2013-02-15 06:27:08

标签: javascript html5 settimeout

我已经解决了很多个小时,但我不知道为什么我无法解决它。我有以下代码:

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 个答案:

答案 0 :(得分:1)

尝试添加“return false;”在您的计时器功能中调用停止后。因为现在我认为你将停止计时器,但是你将在功能的底部再次启动它。

if (that.min === 1 && that.sec === 40) {
   that.bug.gotoAndPlay('take_photo');
   that.flower.onPress = null;
   stop();
   return false;
}