npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tanzerfe/tasktimer

v1.0.0

Published

A timer manager lib

Downloads

3

Readme

@tanzerfe/tasktimer

@tanzerfe/tasktimer 是一个轻量级 TypeScript 库,用于管理全局计时任务。支持动态注册、卸载回调函数,并通过分组管理回调任务。

特性

  • 动态注册回调: 根据时间间隔动态注册回调函数
  • 循环调用: 支持回调函数的循环执行
  • 分组管理: 可以通过分组标识管理和卸载一组回调函数

安装

# 使用 pnpm 或使用 npm、yarn
pnpm install @tanzerfe/tasktimer

使用

### 注册回调函数

你可以使用 `registerCallback` 方法来注册一个回调函数。此方法会返回一个唯一的 ID,用于后续的动态卸载。

```typescript
import { TaskTimer } from '@tanzerfe/tasktimer';

// 注册一个每秒执行的回调,并立即执行一次
const id1 = TaskTimer.registerCallback(() => {
  console.log('Callback every 1 second, with immediate and repeat');
}, { time: 1000, log: true});

// 注册一个2秒钟后执行的回调,仅执行一次
const id2 = TaskTimer.registerCallback(() => {
  console.log('Callback after 2 seconds, one time, with immediate');
}, { time: 2000, repeat: false, immediate: false });

动态卸载回调函数

可以通过唯一的 ID 来卸载一个回调函数:

setTimeout(() => {
  TaskTimer.unregisterCallback(id1);
  console.log('Unregistered the first callback');
}, 5000);

通过分组卸载回调函数

还可以通过分组标识来卸载一组回调函数:

TaskTimer.registerCallback(() => {
  console.log('Grouped Callback');
}, {
  time: 1000,
  group: 'group1',
  repeat: true,
  log: true
});

// 卸载分组中的所有回调函数
TaskTimer.unregisterCallbackByGroup('group1');

重置/销毁计时器

调用 destroy 方法来停止所有计时任务并清空回调函数:

TaskTimer.destroy();
console.log('All callbacks cleared and timer stopped.');

配置项

在注册回调函数时,可以通过 CallbackOptions 对象传递以下配置项:

  • time (number, 必填): 指定回调函数的执行时间间隔(毫秒)。
  • group (string, 可选): 用于对回调函数进行分组管理的标识符。默认值为 'default'
  • immediate (boolean, 可选): 是否在计时开始前立即执行回调函数。默认值为 true
  • repeat (boolean, 可选): 是否重复执行回调函数。默认值为 true
  • log (boolean, 可选): 是否在控制台中记录回调函数的执行情况。默认值为 false

方法

  • registerCallback(callback: () => void, options: CallbackOptions): string: 注册一个回调函数,并返回一个唯一的 ID。
  • unregisterCallback(id: string): void: 卸载指定 ID 的回调函数。
  • unregisterCallbackByGroup(group: string): void: 卸载指定分组的所有回调函数。
  • destroy(): void: 停止所有回调函数,并清空计时器。