开发者

Synchronizing with server time - jquery countdown plugin and php

开发者 https://www.devze.com 2023-03-01 17:40 出处:网络
I have a following code in my php file: <div id=\"clockTimestamp\" class=\"hidden\"><?=$_SERVER[\'REQUEST_TIME\'];?></div>

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

精彩评论

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