【发布时间】:2019-08-29 19:26:39
【问题描述】:
我正在尝试使用 setInterval 显示一个每秒上升的计时器。
该变量似乎在每次循环时都会自行重置;结果是 HTML 页面上的显示没有改变。
我已将变量移到函数之外,但这不起作用。不太确定从这里去哪里。
this.intervalFour = setInterval(() => this.totalTime(this.secs, this.restSecs), 1000);
totalTime(seconds, rests) {
var fullTime = seconds * 8 + rests * 7;
fullTime--;
var secCounter: any = 0;
var minCounter: any = 0;
secCounter++;
if (secCounter < 10) {
secCounter = "0" + secCounter;
}
if (minCounter < 10) {
minCounter = "0" + minCounter;
}
if (secCounter == 60) {
secCounter = 0;
minCounter++;
}
if (fullTime = 0) {
clearInterval(this.intervalFour);
}
this.m.innerHTML = minCounter;
this.s.innerHTML = secCounter;
console.log(secCounter, minCounter, "test");
}
我知道这是一件很愚蠢的事情,但我找不到让 secCounter 在每个循环中上升一个的解决方案。
【问题讨论】:
标签: javascript typescript timer setinterval seconds