简体   繁体   English

Javascript:为什么我无法在对象行为中清除超时?

[英]Javascript: Why I can't clearTimeout in my object behavior?

I have been solving this for many hours but I don't know why I can't solve it. 我已经解决了许多小时,但是我不知道为什么我不能解决它。 I have the following code: 我有以下代码:

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);
    }
};

I already put the debugger to find out that my event is fired or not. 我已经把debugger找出我的事件是否被触发。 The result is it is fired correctly as expect but the Timer doesn't stop and keep going forever. 结果是它已按预期正确触发,但计时器不会停止并且永远持续运行。 Can anyone help me I really need your help now. 谁能帮我,我现在真的需要您的帮助。 I'm new to javascript :). 我是javascript新手:)。 Thanks in Advance. 提前致谢。

Try adding "return false;" 尝试添加“ return false;”。 after you call stop in your timer function. 在您调用计时器函数中的stop之后。 As it is now I think you will stop the Timer but then you will start it again at the bottom of the function. 现在,我认为您将停止计时器,但随后将在功能底部再次启动它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM