@colyseus/timer

中文文档
2.0.3发布于 2026/01/10MITTypeScriptESM + CJS近 30 天下载 57.9万

Timing Events tied to @colyseus/clock

原始使用文档
文档目录3 节
  1. Why?
  2. API
  3. License

@colyseus/timer

Timing Events tied to colyseus/clock.

ClockTimer is a subclass of Clock, which adds methods to handle timeout and intervals relying on Clock's ticks.

Why?

Once built-in setTimeout and setInterval relies on CPU load, functions may delay an unexpected amount of time to execute. Having it tied to a clock's time is guaranteed to execute in a precise way.

Therefore there is ideally one single CPU clock for all the timers and then they are executed in sequence.

See a quote from W3C Timers Specification:

This API does not guarantee that timers will fire exactly on schedule. Delays due to CPU load, other tasks, etc, are to be expected.

It does however guarantees an order of « does this timer should execute based on elapsed time since last tick call ? » based on when they were registered.

In classic timer if we have a setInterval of 0ms it will fill the event loop timer queue with callbacks (like A LOT) and if we have another of 1000ms interval it will be executed only after many of the 0ms callbacks are executed. This is not the case with ClockTimer, as it loops through all timers and checks if they should be executed there are more chance that the 1000ms callback will be executed in time.

You can also call .tick() manually to check all the timers at once on a specific important time.

Other cool stuff is that you manage all your timers in one place and therefore you can clear all of them at once. This is useful for example when you want to clear all timers when a game is over. Or when using those is irrelevant.

API

import { ClockTimer } from "@colyseus/timer";
const timer = new ClockTimer();

Clock

  • setInterval(handler, time, ...args) -> Delayed
  • setTimeout(handler, time, ...args) -> Delayed
  • duration(ms: number) -> Promise<void> - Convenience method to wait for a duration in async functions or promises. See associated JSdoc for more details.
  • clear() - clear all intervals and timeouts, and throw any promise created with duration.

Delayed

  • clear() -> void - Clear timeout/interval
  • reset() -> void - Reset elapsed time
  • active -> Boolean - Is it still active?
  • pause() -> void - Pause the execution
  • resume() -> void - Continue the execution
  • paused -> Boolean - Is is paused?

License

MIT

版本与发布记录

@colyseus/timer 共发布 8 个版本,首次发布于 2024/07/18;过去 12 个月发布了 4 个版本。

版本发布日期距今
2.0.3latest2026/01/109 个月前
2.0.22026/01/109 个月前
2.0.12026/01/109 个月前
2.0.02026/01/0910 个月前
1.0.32025/10/151.1 年前
1.0.22025/10/151.1 年前

关于 @colyseus/timer 的常见问题

@colyseus/timer 是什么?

Timing Events tied to @colyseus/clock

如何安装 @colyseus/timer?

在项目目录运行 npm install @colyseus/timer,或使用 pnpm add / yarn add / bun add。

@colyseus/timer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@colyseus/timer 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@colyseus/timer 可以商用吗?

@colyseus/timer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@colyseus/timer 的下载量有多少?

根据 npm 官方下载统计,@colyseus/timer 在 2026/10/11 – 2026/11/09 这 30 天内被下载 579,488 次,日均约 19,316 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@colyseus/timer 还在维护吗?

最新版本 2.0.3 发布于 2026/01/10(9 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 4 个;首次发布于 2024/07/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。