动态服务器时间 [英] dynamic server time

查看:171
本文介绍了动态服务器时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据了解,我无法通过脚本中的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冲突(显示选择菜单不正确)。我补充说,没有冲突代码脚本,但它是无用的..不得不删除prototype.js

解决方案

你的脚本是什么在inc / clock.php上在您的服务器上轮询一个脚本,并且每秒将脚本的输出(粗略地)替换为#clock_time元素的内容。
这应该可以工作,只要你有一个id为clock_element的元素和一个脚本在yoursite.tld / inc / clock.php



然而,我不同意不断轮询服务器是当前的时间。只需将一次同步到您的网络服务器即可。除了一些微小的差异,这应该保持你的时钟同步足够好一段时间。如果您的webapp将运行超过几个小时或几天,您应该定期重新同步您的时钟(每天或每周一次)。



使用Date对象跟踪客户端上的服务器时间。只需从clock.php的输出(有效的日期输出作为先决条件)创建一个Date对象,并根据与远程服务器同步时钟的时间增量,定期更新clock_element(如每秒钟)。



这里有一些粗略的代码,没有测试,可以推荐一些语法错误,但是简要说明你应该做什么:

  function setupServerClock(clock_element,remote_time_url,remote_update_interval,local_update_interval){
var w = window;
//客户端重新同步时间
var ct = new Date();
//重新同步的服务器时间
var st = new Date();
// setup resync
w.setInterval(function(){
jQuery.ajax({
url:remote_time_url,
success:function(data){
ct = new Date();
st = new Date(data);
}
});
},remote_update_interval);
//设置本地时钟显示
w.setInterval(function(){
//自上次重新同步以来,本地机器上传递的时间
var delta = new Date() - ct;
//我们假设在服务器上通过的时间是
//(是的,我知道,在服务器上空间可能不一样
//位置和搁置时钟非常不准确)
var clock = st - 0 + delta; // - 0转换为微秒时间戳
jQuery(clock_element).html(new Date(clock));
} ,local_update_interval);
}

通过以下方式调用:

  setupServerClock(jQuery('#clock_element'),'inc / clock.php',1000 * 60 * 60,1000); 

这将设置要写入#clock_element的时钟,使用从yourdomain.tld / inc / clock.php,每小时重新同步时钟,并每秒钟更新时钟的本地表示。



哦,如果那个定期重新同步确实带来了跳时钟可以考虑简单地给用户反馈,他的时钟被更新,例如这样

  w.setInterval(function (){
jQuery(clock_element).html('resyncing clock ...');
jQuery.ajax({
url:remote_time_url,
success:function(data) {
ct = new Date();
st = new Date(data);
}
});
},remote_update_interval)


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

解决方案

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:

function setupServerClock( clock_element, remote_time_url, remote_update_interval, local_update_interval ) {
    var w = window;
    // client time on resync
    var ct = new Date();
    // server time on resync
    var st = new Date();
    // setup resync
    w.setInterval( function() {
        jQuery.ajax( {
            url: remote_time_url,
            success: function (data) {
                ct = new Date();
                st = new Date(data);
            }
        });
    }, remote_update_interval);
    // setup local clock display
    w.setInterval( function() {
        // the time passed on our local machine since the last resync
        var delta = new Date() - ct;
        // we assume the same time passed at the server
        // (yeah, I know, spacetime might not be the same at the servers 
        // place and off the shelve clocks are pretty inaccurate)
        var clock = st - 0 + delta; // - 0 to convert to microsecond timestamp
        jQuery(clock_element).html(new Date(clock));
    }, local_update_interval);
}

Call it with something like:

setupServerClock( jQuery('#clock_element'), 'inc/clock.php', 1000 * 60 * 60, 1000 );

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

    w.setInterval( function() {
        jQuery(clock_element).html('resyncing clock...');
        jQuery.ajax( {
            url: remote_time_url,
            success: function (data) {
                ct = new Date();
                st = new Date(data);
            }
        });
    }, remote_update_interval);

这篇关于动态服务器时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆