我如何使用6位数的时间戳进行hh:mm:ss倒计时?

问题描述 投票:0回答:1

我进行了倒数计时,但无法正常工作。请运行以下工作片段。

     tiempoUltimoCambioScrubber()

     function tiempoUltimoCambioScrubber() {
    
                var resJson;
                var now;
                var remainingTime;
                var lastChange;
                var dateux;
                var resultx;
                var trueOrFalse;
                var dateObj;
                var m;
                var h;
                var s;
                var timeString;
      
      
                setInterval(function () {
                       // the below response is the result of a HTTP request 
                        response = [ {
                            dataType : "BINARY",
                            value : true,
                            timestamp : 1590724809944,
                            annotation : null
                          }];
      
                        resJson = response;
      
                        lastChange = resJson[0].timestamp;
      
                        trueOrFalse = resJson[0].value;
      
                        now = new Date().getTime();
      
                        remainingTime = now - lastChange;
      
                        // $scope.remainingTime = remainingTime;  
      
                        dateObj = new Date(remainingTime * 1000);
                        h = dateObj.getUTCHours();
                        m = dateObj.getUTCMinutes();
                        s = dateObj.getSeconds();
      
                        timeString = h.toString().padStart(2, '0') + ':' +
                          m.toString().padStart(2, '0') + ':' +
                          s.toString().padStart(2, '0');
      
                        // $scope.timeString = timeString;
      
                        console.log(timeString); 
      
                        // if (!trueOrFalse) {
                        //     // Here I'll reset the counter to 0
                        // }               
        
                }, 1000);
              }

减法最多对应15分钟的时间戳。因为response对象每15分钟更新一次; remainingTime = now - lastChange;始终是一个非常类似于此的值:540519 ..一个6位数的数字。而且我的计数器在像这样的微小时间戳下表现得很疯狂...我如何设法使此计数器在这个很小的[C0 ]变量?

谢谢。

javascript timestamp countdown
1个回答
0
投票

您的问题是创建remainingTime时将remainingTime乘以1000,但是它是已经毫秒值,因此不需要它。将该行更改为

dateObj

并且计数器正确地以秒为单位上升(由您对dateObj = new Date(remainingTime); 的输入定义)。

setInterval
© www.soinside.com 2019 - 2024. All rights reserved.