jasonzissman / TimeMe.js

A JavaScript library to accurately time how long a user views a web page, disregarding idle time and time when the tab or window is minimized.
MIT License
592 stars 124 forks source link

Timer needs to be reinitialized after being reset. How can I just set the timer to zero? #42

Open yoderjen opened 7 years ago

yoderjen commented 7 years ago

I save time the user spends working on a project on my page. Because saving to my database isn't that reliable just on unload, I also save the time spent every time the user saves their project.

So, after a user saves their project, I send that time to be saved to my database, and want to reset the timer to zero.

I thought that's what ResetRecordedPageTime(my page name) would do. What it actually seemed to do is remove the timer altogether - if I reset the page time, then wait, then try to retrieve time spent, I get NaN.

What I'm doing is calling ResetRecordedPageTime(mypage), then re-initializing the timer (same page name every time). This seems to work and have the behavior I want.

Should ResetRecordedPageTime destroy the timer? The docs should indicate this, if so. And it would be nice to have a function that lets you set a timer value to whatever you want so you can zero it out after saving the time saved.

yoderjen commented 7 years ago

I just thought of another thing it would be nice to have - instead of just being able to reset a timer, I'd like to be able to set its value.

then I could call that to set the timer to zero after a save.

On a page refresh, I can save the old timer value to localStorage, and on page initialization I could pull out that value and add it to the timer. Then I would update times only on project save.

Any hope of a "setTimerToValue('page_name', value) function?

yoderjen commented 7 years ago

Ah, I read the code and found out my problem (one of my problems). After calling ResetRecordedPageTime, you don't want to reinitialize the timer - you need to call startTimer('timer-name'). If you re-initialize the timer again with the same name, the idle time gets all screwy.