Skip to content

Instantly share code, notes, and snippets.

@takumifukasawa
Created November 11, 2022 08:50
Show Gist options
  • Save takumifukasawa/b0dfa91c5030cded88a13cbdc4bbb66a to your computer and use it in GitHub Desktop.
Save takumifukasawa/b0dfa91c5030cded88a13cbdc4bbb66a to your computer and use it in GitHub Desktop.
export class TimeSkipper {
targetFPS;
#callback;
#lastTime;
constructor(targetFPS, callback) {
this.targetFPS = targetFPS;
this.#callback = callback;
}
// time [sec]
start(time) {
this.#lastTime = time;
}
// time [sec]
exec(time) {
const interval = 1 / this.targetFPS;
if((time - interval) >= this.#lastTime) {
const elapsedTime = time - this.#lastTime;
const n = Math.floor(elapsedTime / interval);
const deltaTime = interval * n;
this.#lastTime += deltaTime;
this.#callback(this.#lastTime, deltaTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment