javascript - How can I convert seconds to minutes in jQuery while updating an element with the current time? - Stack Overflow

So I see a number of ways to display allot of seconds in a (static) hrminsec.However, I am trying t

So I see a number of ways to display allot of seconds in a (static) hr/min/sec. However, I am trying to produce a visual count down timer:

$('#someelement').html(minCounter + ' minutes ' + ((secCounter == 0) ? '' : (secCounter + ' seconds')));

My counter is reduced inside a SetInterval that triggers ever 1 second:

//.......
var counter = redirectTimer;
jQuery('#WarningDialogMsg').html(minCounter + ' minutes ' + ((secCounter == 0) ? '' : (secCounter + ' seconds')));

//........

SetInternval( function() {
    counter -= 1;
    secCounter = Math.floor(counter % 60);
    minCounter = Math.floor(counter / 60);

//.......

  $('#someelement').html(minCounter + ' minutes ' + ((secCounter == 0) ? '' : (secCounter + ' seconds')));
}, 1000)

It is a two minute counter but I don't want to display 120 seconds. I want to display 1 : 59 (and counting down).

I have managed to get it to work using the above, but my main question is: is there a more elegant way to acplish the above? (note: I am redirecting once "counter == 0").

So I see a number of ways to display allot of seconds in a (static) hr/min/sec. However, I am trying to produce a visual count down timer:

$('#someelement').html(minCounter + ' minutes ' + ((secCounter == 0) ? '' : (secCounter + ' seconds')));

My counter is reduced inside a SetInterval that triggers ever 1 second:

//.......
var counter = redirectTimer;
jQuery('#WarningDialogMsg').html(minCounter + ' minutes ' + ((secCounter == 0) ? '' : (secCounter + ' seconds')));

//........

SetInternval( function() {
    counter -= 1;
    secCounter = Math.floor(counter % 60);
    minCounter = Math.floor(counter / 60);

//.......

  $('#someelement').html(minCounter + ' minutes ' + ((secCounter == 0) ? '' : (secCounter + ' seconds')));
}, 1000)

It is a two minute counter but I don't want to display 120 seconds. I want to display 1 : 59 (and counting down).

I have managed to get it to work using the above, but my main question is: is there a more elegant way to acplish the above? (note: I am redirecting once "counter == 0").

Share Improve this question asked Dec 29, 2010 at 18:50 pghtechpghtech 3,70211 gold badges49 silver badges73 bronze badges 1
  • That Math.floor in the secCounter = Math.floor(counter % 60); line isn't necessary. It's looking okay anyway. – Scorchio Commented Dec 29, 2010 at 18:56
Add a ment  | 

2 Answers 2

Reset to default 3

Try http://ejohn/blog/javascript-pretty-date/

or this:

var old_date = new Date('Wed Dec 29 2010 21:56:33');
var time_update = dateDiff(old_date, new Date()); // {"diff":119000,"ms":0,"s":59,"m":1,"h":0,"d":0}


function dateDiff(date1, date2) {
    var diff = Date.parse(date2) - Date.parse(date1);

    if (diff < 0) return false;
    var objDate = isNaN(diff) ? NaN : {
        diff: diff,
        ms: Math.floor(diff % 1000),
        s: Math.floor(diff / 1000 % 60),
        m: Math.floor(diff / 60000 % 60),
        h: Math.floor(diff / 3600000 % 24),
        d: Math.floor(diff / 86400000)
    };
    return objDate;
}

I saved time (ahahaha) by using this jQuery countdown plugin by Keith Wood.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信