[英]how can I re-call this function? not experienced with ES6
I don't know how to call counter() a second time after it runs on page load 我不知道如何在页面加载后运行第二次调用counter()
I made a new "instance" of the counter, I think. 我想,我做了一个新的计数器“实例”。 And this does work but there must be a correct way.
这确实有效但必须有正确的方法。 I need to call it in 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();
It does not run if i say counter(); 如果我说counter();它不会运行;
You could have a helper counter
function to return the intervalID
like: 你可以有一个帮助
counter
函数来返回intervalID
如:
counter () {
const interval = setInterval(() => this.countdown(), 1000);
return interval;
}
and in your constructor, you could assign the intervalID
to some variable like: 在构造函数中,您可以将
intervalID
分配给某个变量,如:
this.interval = counter();
Then you can stop the counter by passing the variable holding the intervalID
like clearInterval(this.interval);
然后你可以通过传递持有
intervalID
的变量来停止计数器,如clearInterval(this.interval);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.