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();