# @ionepub/node-timer

> A simple countdown timer module.

Latest version **1.0.0** (published 2018-01-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ionepub/node-timer
pnpm add @ionepub/node-timer
yarn add @ionepub/node-timer
bun add @ionepub/node-timer
```

## 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.0.0 |
| Published | 2018-01-24 |
| First published | 2018-01-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | ionepub |
| Maintainers | ionepub |
| Keywords | countdown, node, nodejs, timer |

## Links

- npm: https://www.npmjs.com/package/@ionepub/node-timer
- Repository: https://github.com/ionepub/node-timer
- Homepage: https://github.com/ionepub/node-timer#readme
- Issues: https://github.com/ionepub/node-timer/issues
- npm.io page: https://npm.io/package/@ionepub/node-timer

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-01-24

## README

# node-timer.js nodejs秒倒计时插件

本插件目的在于方便在nodejs中使用秒级的倒计时。

js版参见：https://github.com/ionepub/js-timer

## 安装

```
npm i @ionepub/node-timer --save
```

## 使用方法

### 基本使用

```
var Timer = require('@ionepub/node-timer');
Timer.run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
});
```

### 配置

插件有4个配置项：

- time 倒计时秒数，整数，默认60（秒）
- format 返回的倒计时数字格式，默认`string`字符串，返回的数字格式为两位字符，如`09`；可选值`int`，返回整数，如`9`
- withHour 返回的倒计时是否计算小时，默认`true`，可选值`false`
- withDay 返回的倒计时是否计算天，默认`true`，可选值`false`

需要注意的是，即使`withHour`或`withDay`设置为false，在回调函数的返回值中，依然会返回`day`和`hour`参数，但是参数值始终为零（即不计算）。

自定义配置的几个方法：

#### #1 使用Timer.init()方法

```
// 使用默认配置
Timer.init();

// 倒计时30秒
Timer.init({time:30});

// 其他例子
Timer.init({time:30, format: 'int', withHour: true, withDay: false});
```

#### #2 直接设置插件暴露的变量

```
Timer.settings.time = 30;
Timer.settings.withDay = false;
```

#### #3 使用Timer.run()方法

Timer.run()方法的第一个参数也支持自定义配置

```
// 使用默认配置
Timer.run();

// 倒计时30秒
Timer.run({time:30});

// 其他例子
Timer.run({time:30, format: 'int', withHour: true, withDay: false});

Timer.run({time:30, format: 'int', withHour: true, withDay: false}, function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
});
```

### Timer.run()

Timer.run()方法支持1-2个参数，第一个参数为配置项对象或回调方法；当第一个参数为配置项时，第二个参数为回调方法。

```
// 使用默认配置
Timer.run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
});

// 无回调
Timer.run({time:30, format: 'int', withHour: true, withDay: false});

// 自定义配置和回调
Timer.run({time:30, format: 'int', withHour: true, withDay: false}, function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
});
```

### Timer.diff()

为了更方便使用，插件提供了`diff()`方法，支持填入开始时间（秒）和结束时间参数，以此设置倒计时瞄数。

`diff()`方法必须传递两个整数，且`startTime`必须小于`endTime`。

```
Timer.diff(1516676419, 1516676457);

Timer.diff(0, 30).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});
```

### 链式调用

插件的`init()`、`run()`和`diff()`方法均支持链式调用，但是需要注意一下调用顺序。

- 如果`init()`或者`diff()`在`run()`之后，这个方法的设置是无效的
- 如果多个方法中都对同一个配置项操作，则后面的操作会覆盖前面的设置

```
Timer.init().run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

Timer.init({time:30}).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

Timer.diff(0, 30).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});
```

分步调用也是可以的：

```
// 倒计时30秒
Timer.init({time:30});
Timer.run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});

// 倒计时40秒
Timer.diff(0, 40);
Timer.run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
});
```

### 多个倒计时

插件支持在同一页面中有多个倒计时，例如：

```
Timer.diff(0,10).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
})

Timer.diff(0,5).run(function(day, hour, minute, second, is_end){
	console.log(day+'d', hour+'h', minute+'m', second+'s', is_end);
	if(is_end){
		console.log("已结束");
	}
})
```

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