【发布时间】:2016-09-10 21:26:54
【问题描述】:
我试图在我的游戏中使用一个按钮来停止 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");
}
【问题讨论】:
-
你能把你的代码作为代码块放在问题中吗?从图像中查看代码非常困难。
-
您应该提供代码。否则,你会收集反对票
-
你去。现在应该更好
标签: javascript settimeout cleartimeout