0
大家好我的工作时钟计时器我写在角码的代码它做工非常精细角2加零为字符串之前数不起作用
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);
}
输出是正确的,但在我想在几秒钟或几分钟或几小时前加零,如果他们是< 10我写这个sec = "0" + sec
它告诉我the String Type Is Not assignable to sec
谢谢你。
创建一个单独的填充功能(如[这个答案](https://stackoverflow.com/a/40717803/1229023)),那么就内使用字符串模板(如'$ {pad(hour)}:$ {pad(min)} ...'等) – raina77ow