kunpolibrary/docs/Timer.md
2025-02-26 17:30:40 +08:00

34 lines
849 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 计时器
### 使用
```typescript
import { GlobalTimer } from 'kunpocc';
// 启动一次性定时器2秒后执行
const timerId1 = GlobalTimer.startTimer(() => {
console.log('2秒后执行一次');
}, 2);
// 启动循环定时器每3秒执行一次执行5次
const timerId2 = GlobalTimer.startTimer(() => {
console.log('每3秒执行一次总共执行5次');
}, 3, 5);
// 启动无限循环定时器每1秒执行一次
const timerId3 = GlobalTimer.startTimer(() => {
console.log('每1秒执行一次无限循环');
}, 1, -1);
// 停止定时器
GlobalTimer.stopTimer(timerId1);
GlobalTimer.stopTimer(timerId2);
GlobalTimer.stopTimer(timerId3);
```
注意事项:
- 定时器的时间间隔单位为秒
- loop 参数说明:
- 0执行一次
- 正整数 n执行 n 次
- -1无限循环