1

我想显示数据库中新插入的数据。我从另一个问题中找到了下面的代码,它做了我想要的,但它只在点击时显示数据。那么有人可以告诉我如何让数据每 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

4 回答 4

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
});
于 2013-11-15T03:07:16.070 回答
0

尝试添加setTimeout

success: function(response){                    
    $("#responsecontainer").html(response);
    setTimeout(success, 5000);
}
于 2013-11-15T03:06:20.733 回答
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); 
            }

        });
    };
于 2013-11-15T03:13:08.890 回答
0

您可以在 onload() 中使用 jquery 的 setInterval() 或 setTimeout();参考以下问题,它的答案很好地解释了你到底需要什么。

每 60 秒调用一次函数

于 2013-11-15T03:15:36.527 回答