📜  次日下午 2 点送达倒计时脚本 - CSS 代码示例

📅  最后修改于: 2022-03-11 14:47:58.939000             🧑  作者: Mango

代码示例1
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) + '.' + pad(secs, 2) + '';
            document.getElementById('countdownTimer').innerHTML = str;

        }, 1000
    );
}