js倒计时

    <div class="timeWrap">
        <span id="day"></span><span id="hours"></span><span id="minute"></span><span id="second"></span></div>

 

    /* 倒计时 */
    function countNum(time) {
       let day = document.getElementById('day')
       let hours = document.getElementById('hours')
       let minute = document.getElementById('minute')
       let second = document.getElementById('second')
       function fillZero(time) {
           return time < 10 ? '0' + time : time
       }
       setInterval(() => {
            let selfSetTime = new Date(time)
            let nowTime = new Date()
            let surplus = (selfSetTime - nowTime) / 1000
            let d = fillZero(parseInt(surplus / 60 / 60 /24))
            let h = fillZero(parseInt(surplus / 60 / 60 % 24))
            let m = fillZero(parseInt(surplus / 60 % 60))
            let s = fillZero(parseInt(surplus % 60))
            day.innerHTML = d
            hours.innerHTML = h
            minute.innerHTML = m
            second.innerHTML = s
       }, 1000);
      
    }
    
    countNum('2021-09-02 16:00:00')    

 

posted @ 2021-09-02 15:44  番茄焖鸡蛋  阅读(28)  评论(0编辑  收藏  举报