I am making an online quiz app having a timer which is implemented through JavaScript. If accidentally, the browser closes due to power failure, I intend to resume the timer from the time-point when the browser was closed. Right now, I am recording time only at the end of the quiz in a database using PHP/AJAX. How can I resume from the exact time when the user closed the browser?
Share
The issue with a power failure is that you will not be notified in advance of the power going out so there is no opportunity to save state upon the power failure.
Thus, the only way to have any idea when the power went out is to be continuously saving state every N interval of time. Then, when the power goes out and is then restored, you can look at the last saved state. It won’t be perfect, but will be the best you can do.
Your three options for saving state are:
The advantage of options 1 and 2 is that it’s all client-side so saves are quick and don’t load your server. The disadvantage of options 1 and 2 are that they could conceivably be manipulated by a client trying to game the system.
There’s a small danger that even the local state in options 1 and 2 might not be saved properly in a power outage, but as best I know most browsers do persist this to disk when it’s saved so that it’s reliable even if the browser later crashes.