js实现时间倒计时

需求:
实时显示pk赛截止时间,当前截止时间为当天22:00

代码都很简单,可以根据自己的需求适当调整

function timerFn(ele){
    var myInterval = setInterval(function () {
        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth();
        var d = date.getDate();
        var time = new Date(y, m, d, 22, 00, 0);
        var num = time.getTime() - date.getTime();
    
        var day = parseInt(num / (24 * 60 * 60 * 1000));
        num = num % (24 * 60 * 60 * 1000);
        var hour = parseInt(num / (60 * 60 * 1000));
        num = num % (60 * 60 * 1000);
        var minute = parseInt(num / (60 * 1000));
        num = num % (60 * 1000);
        var seconde = parseInt(num / 1000);
        hour = hour < 10 ? '0' + hour : hour;
        minute = minute < 10 ? '0' + minute : minute;
        seconde = seconde < 10 ? '0' + seconde : seconde;
        var timeStr;
        if(seconde>=0){
            timeStr ="PK倒计时  "+hour+":"+minute+":"+seconde+"  每天00:00开赛";
            ele.html(timeStr);
        }else{
            timeStr ="PK倒计时  00:00:00  每天00:00开赛";
            ele.html(timeStr);
            clearInterval(myInterval);
        }       
    }, 1000)
}

你可能感兴趣的:(js实现时间倒计时)