ZhangXianQiang
2024-04-18 496f3a16fa0fddd23af01fe6cb4d0709a6ebc8d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
        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');
        
        window.requestAnimationFrame(() => {
            this.tick();
        });
    }
}