export class Timer {
    constructor(time, node) {
        this.time = time;
        this.node = node;
        this.isStarted = false;
        this.fluctuation = 0;
    }
    start() {
        this.isStarted = true;
        this.lastTime = performance.now();
        this.schedule();
    }
    stop() {
        if (this.isStarted) {
            clearTimeout(this.intervalID);
            this.isStarted = false;
        }
    }
    schedule() {
        const now = performance.now();
        const elapsed = now - this.lastTime;
        this.fluctuation = elapsed - this.time;
        this.lastTime = now;
        const toWait = Math.max(0, this.time - this.fluctuation);
        this.intervalID = window.setTimeout(() => this.handleResolve(elapsed), toWait);
    }
    handleResolve(dt) {
        if (this.node) {
            this.node.func(dt / 1000); // Assuming time is in milliseconds
        }
        if (this.isStarted) {
            this.schedule();
        }
    }
}
