JavaScript送货定时器倒计时

时间:2014-10-04 21:33:54

标签: javascript

我的网站上有以下JavaScript,显示订单下次交付的剩余时间。

if (document.getElementById('countdownTimer')) {
    pad = function(n, len) { // leading 0's
        var s = n.toString();
        return (new Array((len - s.length + 1)).join('0')) + s;
    };

    var timerRunning = setInterval(
        function countDown() {
            var now = new Date();
            if ((now.getDay() >= 1) && (now.getDay() <= 5)) { // Monday to Friday only
                var target = 15; // 15:00hrs is the cut-off point
                if (now.getHours() < target) { // don't do anything if we're past the cut-off point
                    var hrs = (target - 1) - now.getHours();
                    if (hrs < 0) hrs = 0;
                    var mins = 59 - now.getMinutes();
                    if (mins < 0) mins = 0;
                    var secs = 59 - now.getSeconds();
                    if (secs < 0) secs = 0;
                    var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
                    document.getElementById('countdownTimer').innerHTML = str;
                }
            }
        }, 1000
    );
}

这个问题是在星期六和星期天它只是一直显示00:00:00。 我想要做的是计算一个周末的小时数,就像在这个网站上做的那样,例如:http://www.nakedwines.com/full_site

JavaScript真的不是我的领域,我完全不知道如何更改代码来执行此操作。任何帮助将不胜感激。

小提琴在这里http://jsfiddle.net/rwet0o5f/

2 个答案:

答案 0 :(得分:2)

我已将now.getDay()移到变量中,现在它应该更具可读性。

weekday周日0,周一1,星期六6。 星期六我们在截止日期前增加48小时, 星期天我们只增加了24小时。

http://jsfiddle.net/37ox54bk/7/

if (document.getElementById('countdownTimer')) {
    pad = function(n, len) { // leading 0's
        var s = n.toString();
        return (new Array( (len - s.length + 1) ).join('0')) + s;
    };

    var timerRunning = setInterval(

        function countDown() {
            var target = 15; // 15:00hrs is the cut-off point
            var now = new Date();

            //Put this in a variable for convenience
            var weekday = now.getDay();

            if(weekday == 0){//Sunday? Add 24hrs
                target += 24;
            }

            if(weekday == 6){//It's Saturday? Add 48hrs
                target += 48;
            }

            //If between Monday and Friday, 
            //check if we're past the target hours, 
            //and if we are, abort.
            if((weekday>=1) && (weekday<=5)){
                if (now.getHours() > target) { //stop the clock
                    return 0;
                }                
            }

            var hrs = (target - 1) - now.getHours();
            if (hrs < 0) hrs = 0;
            var mins = 59 - now.getMinutes();
            if (mins < 0) mins = 0;
            var secs = 59 - now.getSeconds();
            if (secs < 0) secs = 0;

            var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
            document.getElementById('countdownTimer').innerHTML = str;

        }, 1000
    );
}

答案 1 :(得分:1)

试试这个。

当你超过目标时间时,我没有做任何事情,我只是将目标时间增加到指向不同的一天(主要区别在于函数开头的if..else if语句)

function countDown() {
    var now = new Date();
    var target = 15; // 15:00hrs is the cut-off point
    if (now.getDay() == 0) { // Sunday - use tomorrow's cutoff
        target += 24;
    } else if (now.getDay() == 6) { // Saturday - use cutoff 2 days from now
        target += 48;
    } else if (now.getHours() < target) { // missed the cutoff point. Use tomorrow instead
        target += 24;
        if (now.getDay() == 5) { // Friday - use Monday cutoff
            target += 48;
        } 
    }
    var hrs = (target - 1) - now.getHours();
    if (hrs < 0)
        hrs = 0;
    var mins = 59 - now.getMinutes();
    if (mins < 0)
        mins = 0;
    var secs = 59 - now.getSeconds();
    if (secs < 0)
        secs = 0;
    var str = pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>';
    document.getElementById('countdownTimer').innerHTML = str;
}, 1000);

我分道扬琴:http://jsfiddle.net/4vhah8yt/1/