# timer-manager-lib
**Repository Path**: DieHunter/timer-manager-lib
## Basic Information
- **Project Name**: timer-manager-lib
- **Description**: 基于 JS 实现的一个定时任务管理器,可以在 Node 和浏览器环境下使用,你可以轻松地添加、删除、启动、停止和清除定时器,使用同一个 interval 管理全局的 timer,尽可能规避了多个不同的任务复杂性和维护的难度,时间间隔多样性,达到以时间为索引的函数缓存的效果
- **Primary Language**: JavaScript
- **License**: GPL-2.0
- **Default Branch**: master
- **Homepage**: https://www.npmjs.com/package/timer-manager-lib
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2024-01-13
- **Last Updated**: 2024-02-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# timer-manager-lib
#### Introduction
JS based on a timing task manager, can be used in the Node and browser environment, you can easily add, delete, start, stop and clear timers, use the same interval to manage the global timer, as far as possible to avoid the complexity of multiple different tasks and maintenance difficulty, time interval diversity, Achieve the effect of time-indexed function cache
#### Debugging tutorial
1. pnpm i
2. pnpm dev
3. pnpm build
#### Usage instructions
1. Installation
```bash
npm install timer-manager-lib
yarn add timer-manager-lib
pnpm install timer-manager-lib
```
2. Introduce the timer-manager-lib module into the code
##### ESModule
```javascript
import { TimerManager } from "timer-manager-lib";
```
##### Commonjs
```javascript
const { TimerManager } = require("timer-manager-lib");
```
##### UMD
```html
```
3. Create a Timer instance
```javascript
const timerManager = new TimerManager({
type: "interval", // interval Polling timer or frame Frame timer
autoStop: true, // Automatically stops the timer when there is no handle
});
```
4. Add a timer
Use the 'add' method to add a timer. It accepts a callback function (' handle ') and a delay time:
```javascript
const handle = () => {
console.log(" Timer trigger ");
};
const delay = 1000; // Time is measured in milliseconds
const timer = timerManager.add(handle, delay);
```
5. Delete the timer
Use the 'delete' method to delete a timer. The parameter provides the timer object to add the timer:
```javascript
timerManager.delete(timer);
```
6. Clear all timers
Use the 'clear' method to stop and clear all timers:
```javascript
timerManager.clear();
```
7. Automatically stop
By default, the autoStop option is set to true. This means that the timer stops automatically when there are no handlers (handlers are empty). To disable this behavior, set 'autoStop' to 'false' at initialization.
8. Comprehensive usage
```javascript
// Create a timer manager instance
const timerManager = new TimerManager();
// Record the execution step
let count = 0;
// Add a timer item and delete it after executing it once
const timer = timerManager.add(() => {
timerManager.delete(timer);
console.log(performance.now(), "del timer", count++);
}, 1000);
// Delay Add a timer item and reset it after three times
setTimeout(() => {
console.log(performance.now(), "timer2 start");
timerManager.add(() => {
console.log(performance.now(), "timer2", count++);
if (count > 3) {
timerManager.clear();
console.log(performance.now(), "clear");
}
}, 1000);
}, 2000);
timerManager.add(() => {
console.log(performance.now(), "timer3");
}, 500);
```
#### for more TS types and source code comments
https://gitee.com/DieHunter/timer-manager-lib
#### Contribute
1. Fork the local warehouse
2. Star Warehouse
3. Make suggestions
4. Create a Pull Request