动态服务器时间
据我了解,我无法使用脚本中的 settimeout() 获取 IE 中的动态服务器时间。我发现了这个示例:
function timeExam(){
$.ajax({
url : "inc/clock.php",
success : function (data) {
$("#clock_time").html(data);
}
});
var func = function()
{
timeExam();
}
setTimeout(func, 1000);
}
<body onload="timeExam();">
bla bla bla
</body>
也许可以使其工作?
如果没有,您能否建议我一个可在所有浏览器中使用的具有服务器时间的动态时钟?我尝试使用prototype.js制作时钟,但它与IE8中的jquery UI冲突(错误地显示了selectmenu)。我添加了脚本中没有冲突代码,但它没有用......必须删除prototype.js
As I understand there is no way I can get dynamic server time in IE with settimeout() in script.. I found this example:
function timeExam(){
$.ajax({
url : "inc/clock.php",
success : function (data) {
$("#clock_time").html(data);
}
});
var func = function()
{
timeExam();
}
setTimeout(func, 1000);
}
<body onload="timeExam();">
bla bla bla
</body>
Maybe it is possible to make it work?
If not, could you please suggest me a dynamic clock with server time that works in all browsers?! I tried a clock with prototype.js but it conflicted with jquery UI in IE8(showed selectmenu incorrectly). I added that no conflict code to script but it was useless.. had to remove prototype.js
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的脚本的作用是轮询服务器上 inc/clock.php 上的脚本,并(大约)每秒用脚本的输出替换 #clock_time 元素的内容。
这应该可以工作,前提是您有一个 id 是clock_element 的元素,并且在 yoursite.tld/inc/clock.php 有一个脚本。
但是,我不同意不断轮询服务器的当前时间。只需将时间同步到您的网络服务器一次就足够了。除了一些细微的差异之外,这应该可以让您的时钟在一段时间内保持良好的同步。如果您的网络应用程序运行时间超过几个小时或几天,您应该定期重新同步时钟(每天或每周一次即可)。
使用 Date 对象来跟踪客户端上的服务器时间。只需从clock.php的输出创建一个Date对象(作为先决条件的有效日期输出),并根据您将时钟与远程服务器同步的时间增量定期(例如每秒)更新您的clock_element。
这里有一些粗略的代码,未经测试,可能有一些语法错误,但简要显示了您应该做什么:
用类似的方式调用它:
这将使用从 yourdomain.tld/inc/ 返回的值将时钟设置为写入#clock_element clock.php,每小时重新同步时钟并每秒更新时钟的本地表示。
哦,如果定期重新同步确实会导致时钟“跳跃”,您可以考虑简单地向用户提供反馈,表明他的时钟已更新,例如像这样
What your script does is poll a script on your server at inc/clock.php and replace your #clock_time element's contents with the output from the script (roughly) every second.
This should work, provided you have a element with id clock_element and a script at yoursite.tld/inc/clock.php
However, I disagree to constantly poll the server for it's current time. It should suffice to sync time only once to your webserver. Beside some minor differences this should keep your clocks synced well enough for a good while. If your webapp will run more than a couple of hours or days you should periodicly resync your clock (once a day or a week should do).
Use a Date object to keep track of the servertime on your client. Simply create a Date object from the output of clock.php (a valid date output as prerequisite) and update your clock_element periodicly (like every second) according to the time delta from when you synced your clock with the remote server.
Here some rough code, not tested, propably some syntax errors, but briefly shows what you should do:
Call it with something like:
This will setup the clock to be written to #clock_element, using the value returned from yourdomain.tld/inc/clock.php, resync the clock every hour and update the local representation of the clock every second.
Oh and if that periodical resync indeed brings up "jumps" in the clock you could think about simply giving the user feedback, that his clock was updated, eg like this
由于您使用的是 jQuery:
根据需要更改“updateSeconds”。
我使用
setTimeout
而不是setInterval
来完成此操作,因为它更安全一些。如果服务器出现问题,这个请求不会一遍又一遍地堆积注定失败的 HTTP 请求,因为在当前请求成功之前它不会设置新的更新。Since you're using jQuery:
Change "updateSeconds" however you want.
I did this with
setTimeout
instead ofsetInterval
because it's a little safer. If there's a server problem, this one won't pile up doomed HTTP requests over and over again, because it doesn't set up a new update until the current one succeeds.