Tuesday, June 12, 2012

Accurate Javascript Timers

While working on manufacturing today, I finally became fed-up with the fact that my javacript countdown timers in the game weren't very accurate.  I did some Google research and experimented on my own and finally came  up with a satisfactory solution for the timers to stay pretty accurate.

I did it by sending the desired end time derived from the PHP time() function as a variable to the javascript function, then using the Date().time() javascript function for the script to derive how many seconds it has left.  So a timer should never be more than a second out of sync now, but it does rely on the user having their system time set accurately.  This doesn't really matter since the game only shows the timers to the player as a convenience and doesn't actually use them to do any changes to the database.

No comments:

Post a Comment