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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kazura/web-daemon

v0.2.0

Published

web-daemon

Readme

npm size license

@kazura/web-daemon

Web Daemon 是一个用于创建和管理定时任务的工具包,它提供了一种简单的方式来处理需要定期执行的任务。

特性

  • 支持创建定时执行的任务
  • 可以控制任务的执行频率和总次数
  • 支持暂停和恢复任务
  • 提供全局任务管理功能
  • 支持立即执行任务
  • 提供完整的 TypeScript 类型支持

安装

npm install @kazura/web-daemon

使用方法

基本用法

import WebDaemon from '@kazura/web-daemon'

// 创建一个每秒执行一次的任务
const daemon = new WebDaemon(
  (next, daemon) => {
    console.log('Task executed at index:', daemon.index)
    next()
  },
  1000 // 执行间隔(毫秒)
)

// 启动任务
daemon.start()

// 暂停任务
daemon.pause()

// 恢复任务
daemon.start()

限制执行次数

// 创建一个只执行5次的任务
const limitedDaemon = new WebDaemon(
  (next, daemon) => {
    console.log('Task executed at index:', daemon.index)
    next()
  },
  1000,
  5 // 最多执行5次
)

limitedDaemon.start()

立即执行

const immediateDaemon = new WebDaemon((next, daemon) => {
  console.log('Task executed at index:', daemon.index)
  next()
}, 1000)

// 启动时立即执行一次
daemon.start(true)

API 参考

构造函数

constructor(
  task: (next: () => void, daemon: WebDaemon) => void,
  rate: number = 1000,
  len: number = Number.POSITIVE_INFINITY
)
  • task: 需要执行的任务函数
  • rate: 执行间隔(毫秒),默认为 1000
  • len: 最大执行次数,默认为无限

方法

  • start(immediate: boolean = false): void - 启动任务
    • immediate: 是否立即执行一次
  • pause(): void - 暂停任务
  • forceCall(): void - 强制执行一次任务
  • synchronize(): void - 同步任务执行时间
  • isAtEnd(): boolean - 检查任务是否已结束

静态方法

  • pauseAll(): void - 暂停所有运行中的任务

属性

  • index: number - 当前执行次数
  • len: number - 最大执行次数
  • timer: number - 计时器标识
  • paused: boolean - 是否处于暂停状态

注意事项

  1. 任务函数必须调用 next() 回调来继续执行
  2. 使用 forceCall() 会增加 index 计数
  3. 当任务达到最大执行次数时,会自动暂停
  4. 使用 pauseAll() 可以一次性暂停所有运行中的任务

示例

轮询数据

const pollingDaemon = new WebDaemon(
  async (next, daemon) => {
    try {
      const response = await fetch('https://api.example.com/data')
      const data = await response.json()
      console.log('Polled data:', data)
    } catch (error) {
      console.error('Polling error:', error)
    }
    next()
  },
  5000 // 每5秒轮询一次
)

pollingDaemon.start()

动画效果

const animationDaemon = new WebDaemon(
  (next, daemon) => {
    const progress = (daemon.index / 100) * 100
    element.style.opacity = progress + '%'
    if (progress >= 100) {
      daemon.pause()
    }
    next()
  },
  16, // 约60fps
  100 // 执行100次
)

animationDaemon.start()

倒计时

const countdownDaemon = new WebDaemon(
  (next, daemon) => {
    const remaining = 10 - daemon.index
    console.log(`Countdown: ${remaining}`)
    if (remaining <= 0) {
      daemon.pause()
    }
    next()
  },
  1000,
  10
)

countdownDaemon.start()

文档

更多详细信息请查看 文档

许可证

MIT

Author

👤 kazura233

  • Website: https://github.com/kazura233
  • Github: @kazura233

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!