繁体   English   中英

创建秒表,但出错了

[英]create a stop watch falls with error

我有一个秒表,它工作良好,但在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"))

否则请忽略此答案;)

我认为您第二次的计算是错误的。 使用以下语句代替您的语句。

var sec = Math.floor(this.time / 10)%60;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM