AJAX + jQUERY:立即加载div,然后每隔X秒刷新一次

时间:2014-11-01 19:59:09

标签: javascript php jquery ajax

我每隔X秒使用jQUERY + AJAX刷新几个div。我想知道在页面加载后立即(第一次)加载这些div的方法是什么,然后等待(例如30秒)每次刷新。 我已经看到你命名一个函数,然后调用刷新。事实是,我无法弄明白如何使用我的代码解决问题。

以下是我的代码行:

// <![CDATA[
    $(document).ready(function() {
    $.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
    setInterval(function() {

    //DIVs that are being loaded
    $('#item01_tobeloaded').load('items/item01.php');
    $('#item02_tobeloaded').load('items/item02.php');

 }, 30000); // the "30000" here refers to the time to refresh the div. it is in milliseconds.

    });
    // ]]>

任何帮助都将非常感激:)

谢谢!

1 个答案:

答案 0 :(得分:2)

<script type="text/javascript">
     $(document).ready(function() {
       $("#refresh").load("refresh.php");
       var refreshId = setInterval(function() {
          $("#refresh").load('refresh.php?' + 1*new Date());
       }, 1000);
    });
</script>

这个小脚本加载并刷新div&#39; refresh&#39;您可以通过将1000更改为您需要的任何值来持续调整以满足您的需求。 1000会每秒刷新一次。

这一行

$(document).ready(function() {
$("#refresh").load("refresh.php");

在文档准备好后加载内容,之后您可以坚持使用代码