【问题标题】:create a stop watch falls with error创建秒表时出错
【发布时间】:2017-03-08 16:17:21
【问题描述】:

我有一个运行良好的秒表,但在 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)

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    如果您可以选择,请使用moment.js 格式化经过的时间。

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

    否则忽略这个答案;)

    【讨论】:

      【解决方案2】:

      我认为您对第二个的计算是错误的。用下面的语句代替你的。

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

      【讨论】:

      • 你这行得通,但我觉得我的计时器工作得很快
      • 我首先将设置的超时时间更改为 100,它在 10 中运行良好,谢谢您的回答 %60
      • 你能告诉我如何将这个状态 00:00 带到我的计时器任何建议@reza
      • 当您重置计时器时,如果您使用相同的逻辑打印,它将显示 00:00
      • 没有初始计时器在 0:0,它应该像这样开始 01:01, 1min & 1sec
      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 2014-12-16
      • 2017-07-16
      • 1970-01-01
      相关资源
      最近更新 更多