javascript - 创建秒表时出错

标签 javascript typescript

我有一个秒表,效果很好,但在 60 秒后的秒值中,我需要计时器转到零,分钟到 1,并且以同样的方式,每 60 秒分钟应该改变

    /* Stopwatch */
    starttime(){
        console.log("timer started");
        if( this.running == 0){
            this.running = 1;
            this.adder()
        }else{
            this.running = 0;
        }
   }

    reset(){
        console.log("timer reset");
        this.running = 0;
        this.time = 0;
        this.total = 0;
        this.Sec = 0;

    }

    adder(){

        console.log("timer incrementor");
        if(this.running == 1){
            setTimeout(()=>{ 
                this.time++;
                var mins = Math.floor(this.time/10/60);
                var sec = Math.floor(this.time / 10 );
                var tens = this.time/10;

                this.total =  mins + ':' +  sec;
                console.log(this.total) ;
                this.Sec = sec;               
                this.adder()  

            },10)
        }

    }   

但是这里时间发生了变化,秒被累加起来,当它达到 60 时它不会变为零,它会移动到 61,62,63.... 120 秒后的采样时间是 0:2:120,我需要什么是 0:2:0(小时:秒:分钟)

修改“var sec = Math.floor(this.time/10)%60;”后工作正常并将设置超时时间更改为

this.adder()  

                },100)

最佳答案

如果您可以选择,请使用 moment.js设置耗时的格式。

this.total = moment.utc(this.time).format("mm:ss.SSS"))

否则请忽略此答案;)

关于javascript - 创建秒表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42665301/

相关文章:

typescript - 使用 TypeScript 泛型作为自己的参数,或将其指定为自己的默认参数

typescript - 错误 : Integrating terra components into Ionic3 app

Javascript Array Push 中断循环?

angular - primeng数据表从列中获取值

javascript - 如何在一个命令中使用不相似的更新条件更新 MongoDB 中的多个文档?

javascript - Mocha : Error Timeout of 2000ms exceeded

javascript - 用javascript浮点和

javascript - jQuery UI 的可排序不允许放置在父级之外

javascript - 创建 javascript 类时使用什么模式?

html - Angular 9 - 具有相同名称的 formControlName 的不同 formGroupName 影响相同的 formControlName