开发者

Synchronizing with server time - jquery countdown plugin and php

I have a following code in my php file:

<div id="clockTimestamp" class="hidden"><?=$_SERVER['REQUEST_TIME'];?></div>  
<div id="clock"></div>

I am using the jquery countdown plugin from Keith Wood and am calling it like this:

$('#countdown').countdown({until: until, serverSync: getServerTime});

And following code in the js file:

function getServerTime() { 
    var time = $('#clockTimestamp').html()*1000;
    time = new Date(time); 
    return time;
}

Now, my question is: is this approach with the $_SERVER['REQUEST_TIME'] OK or should I do it like this:

function getServerTime() { 
    var time = null; 
    $.ajax(
    {
        url: '../../_ajax/getServerTime.php', 
        async: fals开发者_JAVA技巧e, 
        dataType: 'text', 
        success: function(text) { 
            time = new Date(text);
        }, 
        error: function(http, message, exc) { 
            time = new Date(); 
        }
    }); 
    return time;
}


I'm sure you've solved this now, but for later visitors....

The easiest way to do this would be to use a jQuery server time plugin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜