在JavaScript中使用按钮取消timeOut

cancel timeOut in JavaScript with a button

本文关键字:按钮 取消 timeOut JavaScript      更新时间:2023-09-26

我试图停止setTimeout与一个按钮在我的游戏,但当我点击按钮,我回到菜单和计时器继续。谁能帮帮我?

注。我在中间删去了很多代码,使它更小

       this.stop();
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // gestion du temps
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    this.leTimeOut = window.setTimeout(punir.bind(this), 31000);
    this.lInterval = window.setInterval(afficherTemps.bind(this), 1000);
/////////////////////////
Cutted section
////////////////////////
        console.info("!!! "+nbMatchs+" !!!");
        this.txtItems.text = ""+(6-nbMatchs);
        if(nbMatchs == 6){
            window.clearTimeout(this.leTimeOut);
            this.gotoAndStop("FinNiveau1");
        }
        return bonnePosition;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // Fonction pour lancer la punition
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    function punir(evt){
        console.log("temps écoulé"); 
        this.gotoAndStop("EchecNiveau1");
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // Fonction d'affichage du temps
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    this.txtTimer.text = "29";
    function afficherTemps(evt){
        this.txtTimer.text = parseInt(this.txtTimer.text)-1;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // Fonction et écouteur du bouton Annuler
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    this.btRetour.evenementClick = this.btRetour.on("click", onClicA);
    function onClicA(evt) {
        console.log("retour au menu");
        clearTimeOut(this.leTimeOut);   
        evt.remove(); //retrait de l'écouteur (la méthode facile!)
        this.parent.gotoAndStop("Intro");
    }

如果你的代码完全复制了你的" clearartimeout "