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!
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.net/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.