Set jQuery countdown time from 50000 seconds

671 views Asked by At

i am using jQuery Countdown: http://keith-wood.name/countdown.html

Example usage of this script is:

$(selector).countdown({since: new Date(2010, 12-1, 25)});

You have to set the full date, what if i have only total amount of time in seconds? I mean i want to set countdown from 50000 seconds and automatically transform it in to a days:hours:seconds?

3

There are 3 answers

8
Danny On BEST ANSWER

You would have to get the current date and add 50000 seconds to it. You could do it like the following

$("#countdown").countdown({
    until: new Date(new Date().getTime() + (50000 * 1000))
});

JSFiddle Example

0
andi On

Try this:

var now = new Date();
var later = new Date(now.getTime() + 50000000); // 50000s == 50000000ms
$('#countdown').countdown({until: later});
2
Neta Meta On

I've just written a little function to do a counter for you just incase you want to have more control.

countDown($('#selector'), 300);

function countDown(selector, seconds){
    var oneSecond = 1;
    var passedSeconds = 0;
    var timeLeft = 0;
    var interval = setInterval(
        function(){
            passedSeconds += oneSecond;
            timeLeft = seconds-passedSeconds;
            if(timeLeft > 0 ){
                selector.text(getTimer(timeLeft));
            }else{
                selector.text('Time out');
                clearInterval(interval);
            }

        },
        oneSecond*1000);
}

function getTimer(timeLeft){
    var litteralDuration = '';
    var s = parseInt(timeLeft);
    var d = Math.floor(s / 86400);
    s %= 86400;
    var h = Math.floor(s / 3600);
    s %= 3600;
    var m = Math.floor(s / 60);
    s %= 60;


    if(d > 0){
        litteralDuration += (d == 1) ? '1 D ' :  d + ' Ds ' ;
    }
    if(h > 0){
        litteralDuration += (h == 1) ? '1 H ' : h + ' Hs ' ;
    }
    if(m > 0){
        litteralDuration += (m == 1) ? '1 M ' : m + ' Ms ' ;
    }
    if(s > 0){
        litteralDuration += (s == 1) ? '1 S ' :  s + ' Ss ' ;
    }

    return litteralDuration;
}

Here is a JS Fiddle for it: Fiddle

hope that helps