Record Total Time Played

Hey everyone,

For my latest game, I’d like to record the total time the player has played the game. How would I do this? Ideally I’d like to store it in localStorage.

Hi @DevilZ,

You can add a timer in a script update method to keep track of the playing time (make sure to set it to zero in your initialize method when the game starts):

this.timer += dt;

To save it to local storage check the following article:

1 Like

Thanks a lot @Leonidas, localStorage I am adept at, just wanted to know the best method that’ll work with it efficiently. Many thanks again!

1 Like

Wouldn’t it be better to just record the start and end time using javascript’s Date function and subtract that to get the play time, instead of incrementing a variable in the update loop every frame?

Hi @Gamer_Wael,

Yes, I’d already thought of that, but it would have no guarantee of being saved since if the user just closes the page, that’s why I didn’t use it.

interval() can conceivably also be used for performance gains, https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

1 Like