我有一个 php 页面,它从 mysql 查找事件事件并循环结果,然后打印详细信息。它找到事件名称、事件开始时间和加入的用户总数,但页面的数据仅在用户重新打开页面时刷新,我想要的只是加入的用户列在一段时间后更新。
$result=fetchEventList();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class=\"row\">
<div class=\"td td1\">".$row["EventName"]."</div>
<div class=\"td td2\">".$row["StartTime"]."</div>
<div class=\"td td3\">".$row["JoinedUsers"]."</div>";
}
}
我使用 javascript 搜索并找到了许多答案并仅加载 div,但它对我不起作用,我尝试过
<script type = \"text/javascript\">
setInterval(\"my_function();\", 5000);
function my_function(){
$('.td3').load(location.href + ' #time');
}
但它不起作用,任何人都可以插入我遵循的方向,我也不想再次刷新整个页面,只是所有事件的加入用户 div
最佳答案
您不能仅“重新加载”div。这是一个 5 秒后为用户刷新页面的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
setTimeout(function() {
location.reload();
}, 5000);
</script>
</body>
</html>
要仅更新 div 上的内容(无需刷新页面),您需要 AJAX 调用。
在 JQuery 中,您可以使用 $.get()
方法并使用 $(divElem).html(result)
设置内容。
类似于:
// Set interval will repeat at a specified time interval
// in this case, every 5 seconds
setInterval(function () {
// Our AJAX call to get the updated PHP results
$.get('yourPage.php', function(result) {
// Put the results in the div
$(yourDiv).html(result);
});
}, 5000);
关于javascript - 间隔一段时间后用来自 mysql 的数据重新加载 PHP 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59999229/