javascript - countdown timer using moment.js mm:ss format - Stack Overflow

I am using this moment.jsHere's the sample code:var mytimer= moment().format('00:60');

I am using this moment.js

Here's the sample code:

var mytimer= moment().format('00:60');

setInterval(counter,500);

function counter(){
     mytimer--; // its return NaN error
     sym.$("Text").html(mytimer);
}

How can I make it count down like this?

00:60 00:59 ... 00:00

Thanks in advance!

I am using this moment.js

Here's the sample code:

var mytimer= moment().format('00:60');

setInterval(counter,500);

function counter(){
     mytimer--; // its return NaN error
     sym.$("Text").html(mytimer);
}

How can I make it count down like this?

00:60 00:59 ... 00:00

Thanks in advance!

Share Improve this question edited Aug 21, 2015 at 13:30 Vignesh Pichamani 8,07022 gold badges80 silver badges117 bronze badges asked Aug 21, 2015 at 13:02 Budgie JacksonBudgie Jackson 211 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You don't need to use moment in this case. It strikes me as a library better suited for formatting dates, not timers. Please see my example below. It uses a callback so you can reuse this to do any number of things.

    function MyTimer(callback, val) {
        val = val || 60; 
        var timer=setInterval(function() { 
            callback(val);
            if(val-- <= 0) { 
                clearInterval(timer); 
            } 
        }, 1000);
    }
    new MyTimer(function(val) {
        var timerMsg = "00:" + (val >= 10 ? val : "0" + val);
        document.getElementById("timer").textContent = timerMsg; 
    });
<div id="timer"></div>

I know this is not using moment, however this is in the format you want

Check this out.

http://jsfiddle/robbmj/czu6scth/2/

window.onload = function() {

      var display = document.querySelector('#time'),
          timer = new CountDownTimer(5);

      timer.onTick(format).onTick(restart).start();

      function restart() {
        if (this.expired()) {
          setTimeout(function() { timer.start(); }, 1000);
        }
      }

      function format(minutes, seconds) {
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = minutes + ':' + seconds;
      }
    };

This will display in 00:00 format.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248204a4618514.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信