Jquery倒计时服务器同步问题

时间:2011-07-20 22:20:06

标签: php jquery time clock countdown

我正在尝试为活动创建倒计时。我正在使用Jquery Countdown

我有这段代码:

$(function () {
    var fecha = new Date("July 30, 2011 00:00:00");
    $('#defaultCountdown').countdown({
        until: fecha,
        format: 'DHMS',
        expiryUrl: "http://www.google.com",
        serverSync: serverTime,
        timezone: -4
    });


});


function serverTime() {
    var time = null;
    $.ajax({
        url: 'serverTime.php',
        async: false,
        dataType: 'text',
        success: function (text) {
            time = new Date(text);
        },
        error: function (http, message, exc) {
            time = new Date();
        }
    });
    return time;
}

脚本工作正常,但当我尝试更改时钟日期时,倒计时会发生变化。 知道为什么吗?

3 个答案:

答案 0 :(得分:1)

我想你在服务器上创建了serverTime.php文件? http://keith-wood.name/countdown.html Tab Timezones具有您需要添加到serverTime.php的PHP代码,供您的脚本使用。也可能想要完全符合url的条件:'http:yourdomain.com/serverTime.php'但是使用它应该使用你的服务器时间而不是你当地的PC时间。如果您的服务器在本地PC上,那么......它会改变。

答案 1 :(得分:0)

我带着他们的例子去了他们的网站,改变我的系统时间也会影响他们的倒计时。他们的代码依赖于本地系统时间。

答案 2 :(得分:0)

当您从ajax调用获得服务器时间时,我看到的是,它没有创建JavaScript日期对象。

我搜索过,下面为我工作过。

// Split timestamp into [ Y, M, D, h, m, s ]
var t = "2010-06-09 13:12:01".split(/[- :]/);

// Apply each element to the Date function
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);

alert(d);
// -> Wed Jun 09 2010 13:12:01 GMT+0100 (GMT Daylight Time)