# get-countdown

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

Latest version **1.1.5** (published 2018-12-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install get-countdown
pnpm add get-countdown
yarn add get-countdown
bun add get-countdown
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.5 |
| Published | 2018-12-29 |
| First published | 2016-12-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | 杨君华 |
| Maintainers | yangjunhua |
| Keywords | countdown, setTimeout, setInterval, timeout |

## Links

- npm: https://www.npmjs.com/package/get-countdown
- Repository: https://github.com/yjh30/get-countdown
- Homepage: https://github.com/yjh30/get-countdown#readme
- Issues: https://github.com/yjh30/get-countdown/issues
- npm.io page: https://npm.io/package/get-countdown

## Dependencies (1)

- [loop-setTimeout](https://npm.io/package/loop-setTimeout.md) ^1.0.5

## Recent versions

- 1.1.5 (latest) — 2018-12-29
- 1.1.4 — 2018-12-26
- 1.1.3 — 2018-12-15
- 1.1.2 — 2018-12-15
- 1.1.1 — 2017-03-09
- 1.1.0 — 2016-12-02
- 1.0.6 — 2016-12-01
- 1.0.5 — 2016-12-01
- 1.0.4 — 2016-12-01
- 1.0.0 — 2016-12-01

## README

# get-countdown
> 轮询获取剩余毫秒倒计时长

## installation
npm i -S get-countdown

## Example
```js
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
```js
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
```

---
_Source: https://npm.io/package/get-countdown · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
