1.1.5 • Published 5 years ago

get-countdown v1.1.5

Weekly downloads
5
License
ISC
Repository
github
Last release
5 years ago

get-countdown

轮询获取剩余毫秒倒计时长

installation

npm i -S get-countdown

Example

const getCountdown = require('get-countdown')
getCountdown(60 * 1000, 1000, (countdownMs, timeoutId) => {
  // clearTimeout(timeoutId) // 清除定时器后,callback将不再执行
  // 当countdownMs小于等于0时,定时器自动清除,callback将不再执行
  console.log(`剩余倒计时长:${Math.ceil(countdownMs / 1000)}秒`)
})

source code

const loopSetTimeout = require('loop-setTimeout')

/**
 * getCountdown 轮询获取剩余毫秒倒计时长
 * @param  {Number} durationMs   倒计时持续时间(毫秒)
 * @param  {Number} timeout      定时延时时间(毫秒)
 * @param  {Function} loopCallback
 * @return {undefined}
 */
function getCountdown (durationMs, timeout = 1000, loopCallback) {
  let startTime = Date.now()

  const callback = timeoutId => {
    let nowTime = Date.now()
    durationMs = durationMs - (nowTime - startTime)
    startTime = nowTime

    try {
      loopCallback && loopCallback(durationMs, timeoutId)
    } catch(error) {
      console.log(error)
    }
    if (durationMs <= 0) {
      clearTimeout(timeoutId)
    }
  }

  loopSetTimeout(callback, timeout)
}

export default getCountdown
1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago