在Firefox中,jQuery倒计时不再正确

时间:2014-10-16 07:49:23

标签: jquery firefox countdown

我已经使用Keith Wood的jQuery Countdown插件已经有一段时间了,并取得了成功。但是几天之后,我收到了Firefox用户的抱怨,倒计时时间错误。

一些调查指出,当使用Firefox的人打开我们的网站时,服务器时间会被正确读取,并被Countdown缓存。目前,仅在Firefox中,它一直指的是首次打开网站的服务器时间。它没有考虑到时间已经过去。然而,它在其他浏览器中正确地做到了这一点(并且它曾经在FF中运行得很好)。

例如,如果我现在打开我的网站并将其保持打开状态,那么请在4小时内查看倒计时页面,这将是4个小时。

这是倒计时的代码:

                    $("#final-countdown-values").countdown({
                        until: theEnd,
                        serverSync: serverBasedTime,
                        compact: true,
                        layout: $("#layout-placeholder").html(),
                        onExpiry: doSomething
                    });

这是读取servertime的函数:

function serverBasedTime() {

    $.ajax({url: '/ajaxCalls/servertime',
        async: false, dataType: 'json',
        success: function (data) {
            time = new Date(data);
        }, error: function (http, message, exc) {
            time = new Date();
        }});
    return time;

}

就像我说的,这已经多年没有出现故障了。问题是最近的。

1 个答案:

答案 0 :(得分:0)

好吧,正如Jamie Dunstan指出的那样,问题可能是由Firefox的新HTTP缓存引起的。版本32有问题,第33节也是如此。解决方案是在ajax调用中添加“cache:false”:

function serverBasedTime() {

    $.ajax({url: '/ajaxCalls/servertime',
        async: false, dataType: 'json', cache: false ,
        success: function (data) {
            time = new Date(data);
        }, error: function (http, message, exc) {
            time = new Date();
        }});
    return time;

}

.countdown本身仍然为正在运行的实例缓存serverBasedTime,所以不像我担心的那样(参见原始问题的评论),当15秒“主”时,ajax请求的时间不会一遍又一遍地重复ajax请求运行。