JavaScript倒计时器

时间:2013-03-11 22:01:02

标签: javascript

我想让这个JavaScript倒数计时器显示01而不是1和00,当时该部分没有任何内容。我基本上只是想让它看起来像一个数字时钟,但是当它没有0时它看起来很奇怪。

继承我发现的剧本:

// JavaScript Document

CountDownTimer('03/25/2013 9:0 AM', 'countdownSpring');
CountDownTimer('06/10/2013 9:0 AM', 'countdownSummer');
CountDownTimer('11/27/2013 9:0 AM', 'countdownFall');
CountDownTimer('12/23/2013 9:0 AM', 'countdownWinter');

function CountDownTimer(dt, id)
{
    var end = new Date(dt);

    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
    var timer;

    function showRemaining() {
        var now = new Date();
        var distance = end - now;
        if (distance < 0) {

            clearInterval(timer);
            document.getElementById(id).innerHTML = 'CAMP IS HERE!';

            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);

        document.getElementById(id).innerHTML = days + ':';
        document.getElementById(id).innerHTML += hours + ':';
        document.getElementById(id).innerHTML += minutes + ':';
        document.getElementById(id).innerHTML += seconds;
    }

    timer = setInterval(showRemaining, 1000);
}

4 个答案:

答案 0 :(得分:2)

您可以使用以下简单的功能:

function pad(n) {
  return (n<10? '0':'') + n;
}

然后:

document.getElementById(id).innerHTML = pad(days) + ':' + pad(hours) + ':' +
                                        pad(minutes) + ':' + pad(seconds);

document.getElementById(id).innerHTML = [pad(days),pad(hours),pad(minutes), pad(seconds)].join(':');

答案 1 :(得分:0)

嗯,您可以在将值添加到页面之前测试该值并相应地进行更改,即:

var daysString;
if (days == 0) { daysString = "00"; }
else if (days < 10) { daysString = "0" + days; }
else { daysString = "" + days; } 
document.getElementById(id).innerHTML = daysString + ':';

答案 2 :(得分:0)

一种方法是创建一个数字扩展名,如下所示:

Number.prototype.useLeadingZero = function () {
    return (this < 10 ? "0" : "") + this;
};

然后您可以使用以下内容:

document.getElementById(id).innerHTML = days.useLeadingZero() + ':' +
                                        hours.useLeadingZero() + ':' +
                                        minutes.useLeadingZero() + ':' +
                                        seconds.useLeadingZero();

Working demo

答案 3 :(得分:0)

对您的Javascript源的一些更改(我已使用jquery更改)

            var cdTimer;
        function CountDownTimer(dt, elementID)
        {
            var end = new Date(dt);

            var _second = 1000;
            var _minute = _second * 60;
            var _hour = _minute * 60;
            var _day = _hour * 24;

            cdTimer = setInterval(function() { showRemaining(end, _second, _minute, _hour, _day, elementID) }, 1000);
        }   
        function showRemaining(end, _second, _minute, _hour, _day, elementID) 
        {
            var $updateElement = $('#' + elementID);
            var now = new Date();
            var distance = end - now;
            if (distance < 0) 
            {

                clearInterval(cdTimer);
                $updateElement.html('CAMP IS HERE!');

                return;
            }
            var days = Math.floor(distance / _day);
            var hours = Math.floor((distance % _day) / _hour);
            var minutes = Math.floor((distance % _hour) / _minute);
            var seconds = Math.floor((distance % _minute) / _second);

            $updateElement.html( days + ' ' + Right('00' + hours, 2) + ':' + Right('00' + minutes, 2) + ':' + Right('00' + seconds, 2) );
        }
        function Right(str, n)
        {
            if (n <= 0)
            {
                return "";
            }
            else if (n > String(str).length)
            {
                return str;
            }
            else 
            {
                var iLen = String(str).length;
                return String(str).substring(iLen, iLen - n);
            }
        }           
        $(document).ready(function() {
            CountDownTimer('03/25/2013 9:0 AM', 'countdownSpring');
            CountDownTimer('06/10/2013 9:0 AM', 'countdownSummer');
            CountDownTimer('11/27/2013 9:0 AM', 'countdownFall');
            CountDownTimer('12/23/2013 9:0 AM', 'countdownWinter');
        });