SupremeTechnopriest / react-idle-timer

User activity timer component
https://idletimer.dev
MIT License
1.15k stars 143 forks source link

Can idle timer still work when computer is in sleep mode? #281

Closed shkopa1 closed 1 year ago

shkopa1 commented 2 years ago

Hi, thank you for the great repo! In the new version, when i put my computer to sleep (even on the demo in https://idletimer.dev/) - the timer stops. when i return, the timer resumes from the same point it was asleep. (so the time the computer was asleep does not count)

Is there a way to make the timer keep working while in sleep mode? something like - once the computer "awakes" - check if we have reached the timeout since last action, and if so - immediately invoke the onIdle() ?

these are my configurations: { timeout: 180000000 // 5 hours crossTab: true, syncTimers: 200, events: ["keydown", "mousedown"] };

thanks, Danielle

SupremeTechnopriest commented 2 years ago

Hi @shkopa1,

I believe I can accommodate this. I will put it on the roadmap for the next release.

shkopa1 commented 2 years ago

great, thank you @SupremeTechnopriest . when is the next release scheduled for? Is there any quick solution I can use now as a workaround until next release?

SupremeTechnopriest commented 2 years ago

You could track your own timestamps using onAction. I think I could get this out fairly quickly. Maybe this weekend I can carve out some time to get a patch out.

shkopa1 commented 2 years ago

That would be amazing, thank you! looking forward to it!

shkopa1 commented 2 years ago

Hi, are there any updates? :)

SupremeTechnopriest commented 2 years ago

I did a little work on this today, from what I can tell, the existing code should handle this case unless performance.now epoch is being reset when the computer sleeps. I'm adding in a Date.now sanity check as a backup. Will push a release candidate tomorrow for you to test.

shkopa1 commented 2 years ago

I tested it on the Demo in your site too with this scenario: idle time set to 5 min 10:00 - start timer 10:01 - put the computer to sleep (after 1 min) 10:07 - i woke it up (after 6 minutes of sleep) i saw the timer continues from the point it was asleep (as if only 1 min has passed) and it kept counting 4 more minutes, even though in absolute time, it was already 7 minutes after starting the timer, and idle time is set to 5 min.

i would love to get your update to test it tomorrow. thank you.

shkopa1 commented 2 years ago

Hi, was it pushed ?

SupremeTechnopriest commented 2 years ago

Working on a few other things for this release. I'll have it out today.

SupremeTechnopriest commented 2 years ago

@shkopa1 Published as 5.4.2! Reopen this issue if it doesn't fix the problem.

shkopa1 commented 2 years ago

thank you!! i updated the package, but it still does not count the time in sleep (also in the demo). maybe i'm doing something wrong?

here is what i did: i printed from onAction() the getRemainingTime - which supposed to return the remaining time until idle then I put the comp to sleep for 15 min and when i returned i printed again getRemainingTime - and it seems like only 3 min have passed. not 15. so the sleep time does not count for idle.

am i doing something wrong?

thanks.

SupremeTechnopriest commented 2 years ago

Ah, you are using the get functions. So the patch is just going to ensure that onIdle gets called. In v5 I switched to performance.now for more accurate timing, but I may need to switch back to Date.now to make all the functions work with sync. The good thing about performance.now is that changing the clock time wouldn't affect the timers. Maybe I should allow the developer to choose which date functions they want to use.

shkopa1 commented 2 years ago

i'm actually not really using the functions, i just wanted to see if the issue was fixed. but after you comment now - i did another check, without the functions, and it still did not work:

  1. I set the idle time for 10 min
  2. After 1 min i put the computer to sleep
  3. woke it up after 7 min
  4. waited for 2 more minutes - and nothing happened ( i would expect the onIdle method to execute because 10 min has passed, and we reached the idle time)
  5. waited for 7 more min (the sleep time) and then it reached the onIdle method.

meaning - the sleep time still does not count as idle time

SupremeTechnopriest commented 2 years ago

So the act of waking up the computer and focusing the browser is considered user activity (by default) and will reset the timer. If you were to set timeout for 5 mins, and put the computer to sleep for 6 mins, when you come back onIdle should be called.

shkopa1 commented 2 years ago

ok thanks! so i made some more tests:

  1. scenario A: computer is asleep for all the idle time - the minute the comp wakes up it hits the onIdle - that's great!!!
  2. scenario B: the comp is asleep for part time of idle time, then wakes up but still idle - after reaching the idle time nothing happens. but once i click something - it does hit the onIdle. meaning - it does count the sleep time, but the onIdle is called only after being active (returning from sleep does not count as active since i defined the actions i want) i think that is good enough for me :) thank you so much for this!!!
SupremeTechnopriest commented 2 years ago

Yeah its going to require an event to take place in order to do the check. I will try to make it detect the wake event. Im gong to make this work with all the methods as well in the next release.

shkopa1 commented 2 years ago

Hi again, i just realized that this fix (still counting the time in sleep mode) makes the timer not work across tabs, is that right? when there is only one tab - it works (with sleep and without) but when i have more than 1 tab - it suddenly does not reach onIdle. Even when i'm not in sleep mode.

SupremeTechnopriest commented 2 years ago

Hmm. It shouldn't affect cross tab as it stands. I will have a new version out this weekend that uses dates everywhere so we should have more consistent behavior. Can you show me how you are setting up cross tab?

SupremeTechnopriest commented 1 year ago

This has been resolved and will be published in the next release.