使用jquery从数据库自动加载数据

时间:2013-11-15 02:59:51

标签: php jquery

我想显示数据库中新插入的数据。我从另一个问题中找到了以下代码,它完成了我想要的但它只显示了点击时的数据。那么有人可以告诉我如何每5秒自动加载数据?

 <script type="text/javascript">

 $(document).ready(function() {

  $("#display").click(function() {                

  $.ajax({    //create an ajax request to load_page.php
    type: "GET",
    url: "second.php",             
    dataType: "html",   //expect html to be returned                
    success: function(response){                    
        $("#responsecontainer").html(response); 
        //alert(response);
    }

});
 });
});

</script>

<input type="button" id="display" value="Display All Data" />
<div id="responsecontainer" align="center">

4 个答案:

答案 0 :(得分:2)

$(document).ready(function () {

    function load() {
        $.ajax({ //create an ajax request to load_page.php
            type: "GET",
            url: "second.php",
            dataType: "html", //expect html to be returned                
            success: function (response) {
                $("#responsecontainer").html(response);
                setTimeout(load, 5000)
            }
        });
    }

    load(); //if you don't want the click
    $("#display").click(load); //if you want to start the display on click
});

答案 1 :(得分:0)

尝试添加setTimeout

success: function(response){                    
    $("#responsecontainer").html(response);
    setTimeout(success, 5000);
}

答案 2 :(得分:0)

您可以使用函数setTimeout作为计时器来触发。有关详细信息,请查看以下代码:

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

    var loadData = function() {
        $.ajax({    //create an ajax request to load_page.php
            type: "GET",
            url: "second.php",             
            dataType: "html",   //expect html to be returned                
            success: function(response){                    
                $("#responsecontainer").html(response);
                setTimeout(loadData, 5000); 
            }

        });
    };

答案 3 :(得分:0)

你可以在onload()中使用jquery的setInterval()或setTimeout(); 请参考以下问题,其答案完全解释了您的需求。

Calling a function every 60 seconds