我不知道如何在页面加载时再次调用counter()
我想我做了一个新的柜台“实例”。这是可行的,但必须有一个正确的方法。我需要在replay()中调用它。
/**
* @fileoverview demo
*/
class ReVideo {
constructor() {
this.time = 5;
...
this.timer = document.getElementById("update");
this.counter = setInterval(() => this.countdown(), 1000);
this.fader = fader();
this.counter2 = "";
...
this.retry.addEventListener('click', () => this.replay());
...
}
countdown() {
this.time--;
if (this.time > -1) {
this.timer.innerHTML = "seconds remaining: " + this.time;
} else {
this.timer.innerHTML = "You Lose!";
this.watch.style.display = "block";
this.retry.style.display = "block";
clearInterval(this.counter);
clearInterval(this.counter2);
this.coins++;
this.coinCount.innerHTML = "Coins: " + this.coins;
}
this.notice.style.visibility = "visible";
this.notice.innerHTML = "Loaded";
}
...
replay() {
this.time = 5;
this.watch.style.display = "none";
this.notice.style.visibility = "hidden";
fader("Loaded");
this.retry.style.display = "none";
this.counter2 = setInterval(() => this.countdown(), 1000);
this.counter2;
}
...
}
new ReVideo();如果我说counter(),它就不会运行;
发布于 2019-04-14 21:43:42
您可以使用一个辅助counter函数来返回intervalID,如下所示:
counter () {
const interval = setInterval(() => this.countdown(), 1000);
return interval;
}在你的构造函数中,你可以将intervalID赋值给一些变量,比如:
this.interval = counter();然后,您可以通过像clearInterval(this.interval);一样传递保存intervalID的变量来停止计数器
https://stackoverflow.com/questions/55675825
复制相似问题