2017-03-08 92 views
0

我有一個秒錶,它工作良好,但在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(hrs:sec:min)

修改'var sec = Math.floor(this.time/10)%60;並改變設置超時時間到

this.adder() 

       },100) 

回答

0

我想你的計算是錯誤的。使用下面的陳述而不是你的陳述。

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

雅這個工作,但我覺得我的計時器努力快,我比我的定時器,秒錶在我的手機,當mytimer在20秒移動定時器爲:15SE –

+0

我改變了設置的超時時間爲100首先它在10工作很好謝謝你的答案%60 –

+0

你能告訴我怎麼能把這個狀態00:00到我的計時器任何建議@reza –

1

如果您有選項,請使用moment.js格式化已用時間。

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

否則忽略這個答案;)