TimeUtils

纯逻辑

系统时钟与手动可控时钟助手(一致的毫秒/秒时间戳)。

类别数学基础
依赖档位纯逻辑
内部依赖
相关模块
只取这个模块
modules/math/TimeUtils.js

连同其内部依赖一并复制,保留相对目录结构。

源码

export class Clock {
  constructor({ manual = false, nowMs = 0 } = {}) {
    this.manual = manual;
    this.currentMs = nowMs;
  }

  now() {
    return this.manual ? this.currentMs : Date.now();
  }

  nowSeconds() {
    return this.now() * 0.001;
  }

  useSystem() {
    this.manual = false;
    return this;
  }

  useManual(nowMs = this.currentMs) {
    this.manual = true;
    this.currentMs = nowMs;
    return this;
  }

  setMs(nowMs) {
    this.currentMs = nowMs;
    return this;
  }

  advanceMs(deltaMs) {
    this.currentMs += deltaMs;
    return this;
  }
}

export const DEFAULT_CLOCK = new Clock();