admin管理员组文章数量:1292800
I have an auction site that has a javascript timer counting down. For some reason after 15-20 minutes this timer is lagging the actual time by 20-30 seconds. Over a course of 1 hour the javascript countdown timer can be off by atleast 2-3 minutes. This confuses users as he thinks there are still 2-3 minutes for the auction to close. Once a page loads the server gives the remaining time for the auction which is anything under 2 hours hours & javascript starts counting down from there. So my question is 1) Why is the javascript countdown timer lagging the actual time by a few seconds to minutes after some 20-30 minutes ? 2) how can i ensure the timer is synchronized. I dont want to use ajax to get remaining time as there are many other ajax's running. My server uses Php.
I have an auction site that has a javascript timer counting down. For some reason after 15-20 minutes this timer is lagging the actual time by 20-30 seconds. Over a course of 1 hour the javascript countdown timer can be off by atleast 2-3 minutes. This confuses users as he thinks there are still 2-3 minutes for the auction to close. Once a page loads the server gives the remaining time for the auction which is anything under 2 hours hours & javascript starts counting down from there. So my question is 1) Why is the javascript countdown timer lagging the actual time by a few seconds to minutes after some 20-30 minutes ? 2) how can i ensure the timer is synchronized. I dont want to use ajax to get remaining time as there are many other ajax's running. My server uses Php.
Share Improve this question asked Apr 25, 2012 at 19:54 AnitaAnita 1172 silver badges10 bronze badges2 Answers
Reset to default 10Your timer is lagging because you are using setTimeout()
or setInterval()
. They cannot be trusted. Use the Date
object instead to get the accurate, current time.
You don't need to synchronize. Just put the auction end time (in UTC) into a date variable, and count down to it.
Javascript time is probably lagging because of many other things that happening in your javascript. A lot depends on your specific timer implementation but Javascript is single threaded so any other operation will delay your timer.
The only way to sync with server is to send a request. You can't eliminate that. But you could send requests only after some number of counts rather than every time. For example, every minute when timer executes it send AJAX request to synchronize. You can find right synchronization period by experimenting.
本文标签: phpHow to sync javascript countdown timer with serverStack Overflow
版权声明:本文标题:php - How to sync javascript countdown timer with server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741563974a2385612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论