import EventEmitter from "./eventEmitter"; export default class Time extends EventEmitter { constructor() { super(); this.start = Date.now(); this.current = this.start; this.elapsed = 0; this.delta = 16; // 合适的时机执行loop循环 window.requestAnimationFrame(() => { this.tick(); }); } tick() { const currentTime = Date.now(); this.delta = currentTime - this.current; this.current = currentTime; this.elapsed = this.current - this.start; this.trigger('tick'); this.loopId = window.requestAnimationFrame(() => { this.tick(); }); } destroy() { window.cancelAnimationFrame(this.loopId); this.off('tick'); } }