I'm new in JavaScript. I'm currently making an exam application with countdown timer. I've used JavaScript timer with cookie but the problem is when the computer is shutting down the cookie is lost because the computer is frozen by deepfreeze.
My question is how to make countdown timer using database? And how to make it pause the timer when browser is closed and then resume the timer when the browser is re-opened?
What I would do, is use ajax within a setInterval, say every 10 seconds, then you store a value as the max time in the database ( in seconds ).
Every time the ajax call ( jQuery.post() ) you have a server side script that would decrease the time you have in the database by the timeout set in the interval, so for a quick example.
You start with 360 seconds ( 10 min ) then as your javascript interval calls ajax every 10 seconds you decrease that value in the database by 10 second, whenever the server side code is ran.
I do something vary similar to track hours spent entering data.
This way as long as the page is open, and that javascript code runs it will continue to count down, you could return the time remaining and when its less then 0 prompt some kind of message. If they left for a while or the computer "Froze" it would start right back where it left off once the page was loaded again.
The only real issue I see is you would get what is called race conditions unless you had a way to identify each user and give them each their own timers. To expand on that imagine I have 2 users User A and User B, if just user A visits the page everything is fine. But if both A and B use the page at the same time they will both be reducing the time by 10 seconds each for a total of 20 seconds, for each call of their respective ajax requests.
For that you could use some sort of really basic user login setup. So when they return they would put in their username, and that would let you assign the right timer to them.