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

How can I make a hh: mm:ss countdown with a 6 digit timestamp?

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

     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 位数字.. 我的计数器表现得非常疯狂,带有这样的小时间戳......我如何设法让这个计数器与这个小的一起工作remainingTime变量?

谢谢。

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

dateObj = new Date(remainingTime);

并且计数器以秒为单位正确上升(根据您对 setInterval 的输入定义)。

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);
    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);
}