【countDown倒計(jì)時(shí)封裝】使用requestAnimationFrame封裝倒計(jì)時(shí)功能

requestAnimationFrame 介紹

Window:requestAnimationFrame() 方法 - Web API 接口參考 | MDN (mozilla.org)

代碼封裝

// CountDown.js
class CountDown {
      constructor(remain) {
        CountDown.remain = remain
        this.change = () => null
      }

      static remain = null

      static counting = false

      static endTime = null

      static rafId = null

      start() {
        CountDown.endTime = Date.now() + CountDown.remain
        CountDown.counting = true
        CountDown.count(this.change)
      }

      push() {
        CountDown.counting = false
        CountDown.cancelRaf(CountDown.rafId)
      }

      static count(fn) {
        CountDown.rafId = CountDown.raf(() => {
          if (CountDown.counting) {
            const currentRemain = CountDown.getRemain()
            if (!CountDown.isTheSameSecond(currentRemain, CountDown.remain) || currentRemain === 0) {
              CountDown.setRemain(currentRemain)
              fn && fn(CountDown.parseTime(CountDown.remain))
            }
            if (CountDown.remain > 0) {
              this.count(fn)
            }
          }
        })
      }

      static getRemain() {
        return Math.max(this.endTime - Date.now(), 0)
      }

      static setRemain(value) {
        this.remain = value
      }

      // 是否同一秒
      static isTheSameSecond(time1, time2) {
        return Math.floor(time1 / 1000) === Math.floor(time2 / 1000)
      }

      // 格式化時(shí)間,輸出天數(shù)時(shí)分秒
      static parseTime(time) {
        const SECOND = 1000;
        const MINUTE = 60 * SECOND;
        const HOUR = 60 * MINUTE;
        const DAY = 24 * HOUR;

        const days = Math.floor(time / DAY);
        const hours = Math.floor((time % DAY) / HOUR);
        const minutes = Math.floor((time % HOUR) / MINUTE);
        const seconds = Math.floor((time % MINUTE) / SECOND);
        const milliseconds = Math.floor(time % SECOND);

        return {
          total: time,
          days,
          hours,
          minutes,
          seconds,
          milliseconds,
        };
      }

      // 啟用 requestAnimationFrame 倒計(jì)時(shí)
      static raf(callback) {
        return requestAnimationFrame(callback)
      }

      // 取消 requestAnimationFrame 
      static cancelRaf(id) {
        cancelAnimationFrame(id)
      }

    }

使用

<button id="start">開(kāi)始</button>
  <button id="push">暫停</button>

  <div>
    剩余時(shí)間: <span id="days">0</span>天<span id="hours">0</span>時(shí)<span id="minutes">0</span>分<span id="seconds">0</span>秒
  </div>  
<script src="./CountDown.js">
  const countDown = new CountDown(1000 * 60 * 60 * 24 * 60)
    console.log(countDown)
    countDown.change = (date) => {
      console.log(date)
      days.innerText = date.days
      hours.innerText = date.hours
      minutes.innerText = date.minutes
      seconds.innerText = date.seconds
    }
    start.onclick = () => {
      countDown.start()
    }
    push.onclick = () => {
      countDown.push()
      console.log(countDown)
    }
</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容