PDA

View Full Version : Persistent JS Countdown Timer



Bobafart
Mar 15th, 2007, 04:15 PM
Hi, I have coded a JS countdown timer which is all good and fine if the users continues to view the page.

But how would I create a (fairly secure) countdown timer (doesn't have to be JS exclusive, it can include PHP based server-side checks) so that if the user leaves the page and then returns to the page the countdown timer continues uninterrupted?

I ask because I am making a little web game where the user builds units (like an RTS) .. if he/she clicks "build" the 2 minute countdown timer starts, he can then surf to different pages and then when he/she comes back the countdown timer will continue to countdown

Kor
Mar 15th, 2007, 04:29 PM
than probably you need either a cookie and a php application...

Bobafart
Mar 15th, 2007, 11:30 PM
cookies are hackable

I was thinking PHP application.. but how would it be done?

phoenixshade
Mar 16th, 2007, 01:07 AM
If users must log in, I'd probably add a field to the user database called "startTime". This would be set the first time the timer is called. This way, every time the user logs in, you'd call up their startTime with the php program. You could then use js to calculate and display the elapsed time.

The above would be a counter that continues to run even when the user is not logged in. If you want it to stop counting when they leave and continue when they return, then instead of a "startTime" field in the user db, use a "currentTimer" or similar.

If users don't log in, I'd create a database with the same info based instead on IP addresses. Of course, if the user visits from different computers, this would break.