1

当那部分没有任何内容时,我想让这个 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

4 回答 4

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(':');
于 2013-03-11T22:16:27.697 回答
0

Well, you can always test the value before adding it to the page and make changes accordingly, i.e.:

var daysString;
if (days == 0) { daysString = "00"; }
else if (days < 10) { daysString = "0" + days; }
else { daysString = "" + days; } 
document.getElementById(id).innerHTML = daysString + ':';
于 2013-03-11T22:10:54.950 回答
0

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

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

然后你可以像这样使用它:

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

工作演示

于 2013-03-11T22:14:00.903 回答
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');
        });
于 2013-03-11T22:34:31.103 回答