在固定时间使用Jquery刷新Div

时间:2010-03-30 16:28:16

标签: javascript jquery ajax refresh

我有一个PHP脚本告诉我下一班车何时到期,而且此刻我每隔一分钟左右使用jquery将其刷新为div。现在,因为我知道数据发生变化的时间(在总线到来之后),我希望它在此时刷新div(或者仅仅在之后,并不重要)。

我应该指出我对js很新,但这是我到目前为止所做的:

    var nextbustime = $('#bus').contents();
var nextbustime = new Date(nextbustime);
var now = new Date();

var t = nextbustime.getTime() - now.getTime();


var refreshId = setTimeout(function()
{
    $('#bus').fadeOut("slow").load('modules/bus.php?randval='+ Math.random()).fadeIn("slow");
}, t);

div最初加载了php include。当然,我所做的一切根本不起作用。

我是否需要进行一些循环?我需要刷新时间计算器吗?

请帮忙!

提前致谢...

2 个答案:

答案 0 :(得分:0)

由于您仍在使用jQuery,请在此处查看此jQuery插件:jQuery timer plugin

答案 1 :(得分:0)

我从你的陈述中假设代码适用于第一次刷新?你需要做的是处理接下来的几个。由于时间可能会有所不同(显示),setInterval可能不适合您。应该在每次超时结束时再次调用你的函数。

$(document).ready(function() {
    changeToNextBus();
});

function changeToNextBus() {
    var nextbustime = $('#bus').contents();
    var nextbustime = new Date(nextbustime);
    var now = new Date();

    var t = nextbustime.getTime() - now.getTime();

    var refreshId = setTimeout(function()
    {
        $('#bus').fadeOut("slow").
                  load('modules/bus.php?randval='+ Math.random()).
                  fadeIn("slow");
        changeToNextBus();
    }, t);
}