【发布时间】:2018-01-09 15:56:26
【问题描述】:
大家好,我正在研究时钟计时器,我用角码编写了代码,它工作得很好
let timer: string = "0:00:00";
startPause() {
this.play = false;
this.pause = true;
let sec = 0;
let min = 0;
let hour = 0;
setInterval(() => {
if(sec === 59) {
min++;
sec = 0;
}
else if(min > 59) {
hour++;
min = 0;
}
else {
sec++;
}
this.timer = `${hour}:${min}:${sec}`;
}, 10);
}
输出是正确的,但是当我想在秒或分钟或小时之前添加零时,如果它们 sec = "0" + sec 它告诉我the String Type Is Not assignable to sec 谢谢你。
【问题讨论】:
-
创建一个单独的填充函数(如this answer 中所述),然后在字符串模板中使用它(如
${pad(hour)}:${pad(min)}...等)
标签: javascript angular typescript