倒计时1

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>倒计时案例1</title>
    <style type="text/css">
    * {
        margin: 0;
        padding: 0;
    }
    </style>
</head>

<body>
    <div><span id="div_countDown"></span>后恢复原价</div>
    <script type="text/javascript">
    //倒计时
    var countDown = {
        flag: true,
        hour: 0,
        minutes: 0,
        minutesNew: 0,
        seconds: 0,
        show: 0,
        current: 0,
        length: 0,
        showTagId: null,
        // callback: null, 
        countDownInner: function(vTimeLength) {
            if (!this.flag) {
                return;
            }
            var that = this;
            this.current = vTimeLength;
            minutes = Math.floor(vTimeLength / 60);
            seconds = Math.floor(vTimeLength % 60);
            if (minutes >= 60) {
                hour = Math.floor(minutes / 60);
                minutesNew = Math.floor(minutes % 60);
                if (hour < 10) {
                    hour = "0" + hour;
                }
                if (minutesNew < 10) {
                    minutesNew = "0" + minutesNew;
                }
                if (seconds < 10) {
                    seconds = "0" + seconds;
                }
                show = hour + ":" + minutesNew + ":" + seconds;

            } else {
                if (minutes < 10) {
                    minutes = "0" + minutes;
                }
                if (seconds < 10) {
                    seconds = "0" + seconds;
                }
                show = minutes + ":" + seconds;
            }
            document.getElementById(this.showTagId).innerHTML = show;
            vTimeLength = vTimeLength - 1;
            if (vTimeLength > 0) {
                setTimeout(function() { that.countDownInner(vTimeLength); }, 1000);
            } else {
                setTimeout(function() { that.callback(); }, 1000);
            }
        },
        run: function(vTimeLength, showTagId, callback) {
            if (!this.flag) {
                this.flag = true;
                this.countDownInner(this.current);
            } else if (showTagId) {
                this.length = vTimeLength;
                this.showTagId = showTagId;
                this.callback = callback;
                this.countDownInner(vTimeLength);
            }
        }

    };
    //参数 1.倒计时的秒数   2.控件的id    3.时间到了执行的方法
    countDown.run(7200, 'div_countDown', function() { document.getElementById('div_countDown').innerHTML = '结束' });
    </script>
</body>

</html>

效果图:

原文地址:https://www.cnblogs.com/huanghuali/p/8430772.html