JavaScript setTimeout和系统时间的更改会导致问题 [英] JavaScript setTimeout and changes to system time cause problems
问题描述
我注意到如果我将来设置 setTimeout
1分钟,然后将系统时间改为5分钟,那么 setTimeout
函数将在6分钟内触发。
I've noticed that if I set the setTimeout
for 1 minute in the future, and then change my system time to 5 minutes in the past, the setTimeout
function will trigger in 6 minutes.
我这样做是因为我想看看在夏令时更改期间会发生什么系统时钟。
I did this is because I wanted to see what happens during a daylight savings change to the system clock.
我的JavaScript网页使用 setTimeout
函数每隔5秒自动刷新页面,如果夏令时发生,然后页面信息将冻结一个小时。有解决方法吗?
My JavaScript webpage uses a setTimeout
function to automatically refresh the page every 5 seconds, and if daylight savings were to occur, then the page information would freeze for an hour. Is there a workaround?
编辑:我正在使用Ajax更新页面,我不想刷新整个页面。
I am updating the page using Ajax, I dont want to refresh the entire page.
推荐答案
我要做的是从每5秒打开一个新的Ajax请求改为使用 Comet (长轮询)。这是一个更好的选择,因为服务器会在每次需要时将新结果推送到浏览器,这可能是服务器端的每5秒或每次有新信息时。
What I will do is change from opening a new Ajax request every 5 seconds to use Comet (long polling). It is a better option because the server will push the new results to the browser every time that they are needed, this can be every 5 seconds on server side or every time new information is available.
更好的方法是使用网络套接字并让Comet作为后退。使用 Faye 或 socket.oi 。
Even better would be to use web sockets and have Comet as fall back. This is easy to implement with Faye or socket.oi.
还有其他选项,如CometD o Ape。
There are other options, like CometD o Ape.
这篇关于JavaScript setTimeout和系统时间的更改会导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!