使用香草JS将计数结束日期设置为零

时间:2018-12-03 03:05:52

标签: javascript

我正在尝试将以下结束日期设置为零,而不是从零开始倒数。任何帮助将不胜感激。

var deadline = 'Nov 30 2018 23:59:00 GMT-0700';
function time_remaining(endtime){
    var t = Date.parse(endtime) - Date.parse(new Date());
    var seconds = Math.floor( (t/1000) % 60 );
    var minutes = Math.floor( (t/1000/60) % 60 );
    var hours = Math.floor( (t/(1000*60*60)) % 24 );
    var days = Math.floor( t/(1000*60*60*24) );
    return {'total':t, 'days':days, 'hours':hours, 'minutes':minutes, 'seconds':seconds};
}
function run_clock(id,endtime){
    var clock = document.getElementById(id);

    // get spans where our clock numbers are held
    var days_span = clock.querySelector('.countdown__days');
    var hours_span = clock.querySelector('.countdown__hours');
    var minutes_span = clock.querySelector('.countdown__minutes');
    var seconds_span = clock.querySelector('.countdown__seconds');

    function update_clock(){
        var t = time_remaining(endtime);

        // update the numbers in each part of the clock
        days_span.innerHTML = t.days;
        hours_span.innerHTML = ('0' + t.hours).slice(-2);
        minutes_span.innerHTML = ('0' + t.minutes).slice(-2);
        seconds_span.innerHTML = ('0' + t.seconds).slice(-2);

        if(t.total <= 0){ clearInterval(timeinterval); }
    }
    update_clock();
    var timeinterval = setInterval(update_clock,1000);
}
run_clock('countdown',deadline);

https://jsfiddle.net/wvap1fqy/

谢谢!

0 个答案:

没有答案