When my system goes to hibernate mode, the javascript timer stop the countdown and when it comes back it continue with the countdown.
But my problem is that I'd like the countdown to continue when my system goes to hibernate mode. Is there any solution / workaround to achieve that?
This isn't really a javascript issue.
When you close the lid, your laptop is hibernating, meaning the CPU is switched off and cannot calculate things.
You have three options
1) Make the laptop stay on when closing the lid, or don't close the lid
2) Stop doing timer tasks client side, and instead simply start a timer on the server. Depending on what you are trying to achieve, this may or may not be relevant, but it is the only way to know the user will keep their lid open/browser open/machine on etc.
3) Re-work your code so instead of using a timer that says "wait ten seconds", you instead set it to use absolute times, something like the following (which is vague pseudo-code to demonstrate what I mean, not a working solution)
var targetTime;
var running = false;
startTimer(timeInSeconds)
{
targetTime = now() + timeInSeconds;
running = true;
}
while(running)
{
if(now() > targetTime)
{
doTimerThings();
running = false;
}
}
This will not get the timer precisely right, but will fire as soon as possible after the machine is started again. You can change your logic to suit how you wish to handle this (eg handling things differently if the timer is being fired late)
It won't help if you need to fire the timer at exactly the interval... but that simply isn't possible when the computer is off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With