js实现活动倒计时

   let startTime = 1527647143949; // 开始时间
    var time = new Countdown('#timer',startTime);
    function Countdown (el,startTime) {
        this.startTime = startTime || '';
        this.el = el || '';
        this.init = function () {
            var that = this;
            setInterval(function(){
                that.Countdown();
            },1000);
        };
        this.init();
        this.Countdown = function () {
            var endTime = this.startTime+(24*60*60*1000); // 结束时间 
            var timeLeft = endTime - new Date().getTime(); // 剩余时间
            $(this.el).text(this.formatDuring(timeLeft))
        };
        this.formatDuring = function (mss) {
            var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = parseInt((mss % (1000 * 60)) / 1000);
            if (seconds < 10 && seconds > 0) {
                seconds = '0' + seconds;
            } else if (seconds == 0 || seconds > 10) {
                seconds = seconds;
            };
            return hours + ": " + (minutes < 10 ? '0'+minutes : minutes) + ": " + seconds;
        };
    }

猜你喜欢

转载自www.cnblogs.com/Smiled/p/9111713.html