倒计时函数,距指定时间还有多少天多少小时多少分多少秒

发表于 2020-03-24 11:06:29   |   下载附件   |   字体:
倒计时距指定时间多少小时天时分秒倒计时

HTML部分:

<span class="time" dec_time="2157845">还有 <em class="day">00</em> 天 <em class="hour">00</em> 时 <em class="minute">00</em> 分 <em class="second">00</em> 秒</span>

JS代码

// 倒计时函数
countdown = function(timeStamp, secondFn, finishFn) {
    if (typeof timeStamp == 'undefined' || typeof timeStamp != 'number') return;
    var _secondFn = secondFn || function() {},
        _finishFn = finishFn || function() {},
        _timer,
        _timeStamp = timeStamp,
        _day,
        _hour,
        _minute,
        _second;
    function count() {
        _timeStamp = _timeStamp - 1;
        if (_timeStamp <= 0) {
            clearInterval(_timer);
            _day = _hour = _minute = _second = '00';
            _secondFn(_day, _hour, _minute, _second);
            _finishFn();
        }
        _day = Math.floor(_timeStamp / ( 3600 * 24 ));
        _hour = Math.floor((_timeStamp % ( 3600 * 24 )) / 3600);
        _minute = Math.floor((_timeStamp % 3600) / 60);
        _second = parseInt(_timeStamp) % 60;
        //补0
        _day = (_day < 10 ? '0' + _day : _day + '');
        _hour = (_hour < 10 ? '0' + _hour : _hour + '');
        _minute = (_minute < 10 ? '0' + _minute : _minute + '');
        _second = (_second < 10 ? '0' + _second : _second + '');
        _secondFn(_day, _hour, _minute, _second);
    }
    _timer = setInterval(count, 1000);
};

// 执行代码
var dec_time = parseInt($('.v-case .time').attr('dec_time'));
countdown(dec_time,function(_day, _hour, _minute, _second){
    var node = $('.v-case .time');
    node.find('.day').html(_day);
    node.find('.hour').html(_hour);
    node.find('.minute').html(_minute);
    node.find('.second').html(_second);
});