Php 使用jquery从数据库自动加载数据
我想显示数据库中新插入的数据。我从另一个问题中找到了下面的代码,它实现了我想要的功能,但它仅在单击时显示数据。有人能告诉我如何使数据每5秒自动加载一次吗Php 使用jquery从数据库自动加载数据,php,jquery,Php,Jquery,我想显示数据库中新插入的数据。我从另一个问题中找到了下面的代码,它实现了我想要的功能,但它仅在单击时显示数据。有人能告诉我如何使数据每5秒自动加载一次吗 <script type="text/javascript"> $(document).ready(function() { $("#display").click(function() { $.ajax({ //create an ajax request to load_p
<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">
$(文档).ready(函数(){
$(“#显示”)。单击(函数(){
$.ajax({//创建一个ajax请求以加载\u page.php
键入:“获取”,
url:“second.php”,
数据类型:“html”,//希望返回html
成功:功能(响应){
$(“#responsecontainer”).html(响应);
//警报(响应);
}
});
});
});
尝试添加setTimeout
:
success: function(response){
$("#responsecontainer").html(response);
setTimeout(success, 5000);
}
您可以使用函数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);
}
});
};
您可以在onload()中使用jquery的setInterval()或setTimeout(); 参考下面的问题,它的答案很好地解释了你到底需要什么
嗨,阿伦,你的代码每5秒就会命中一次。即使没有任何操作,我们也会每隔5秒向服务器发送不必要的请求。
$(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);
}
});
};