this.maxtime = 45 * 60;//要做多久的倒计时,这里是45s的倒计时
this.timer = setInterval(()=>{this.leftTimer()},1000/60);//
leftTimer() {
if (this.maxtime >= 0) {
let seconds = this.checkTime(Math.floor(this.maxtime / 60))
let seconds2 = this.checkTime(Math.floor(this.maxtime % 60))
this.setState({
sec:seconds,//每次的秒值赋值给展示的state
sec2:seconds2,
})
--this.maxtime
} else{
clearInterval(this.timer)//倒计时结束,清除倒计时
}
}
checkTime(i){ //将0-9的数字前面加上0,例1变为01
if(i<10){ i = "0" + i; }
return i;
}
最后效果