gpt4 book ai didi

javascript - 平滑刷新显示来自不断更新的 MySQL 列的数据的 html 页面

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:26 26 4
gpt4 key购买 nike

由于我们解决了第一个问题,我在这里开一个新线程以更加清楚,谢谢你们:)

我们有一个 AI,它不断地编写文本并将其推送到 MySQL bdd。我们希望将此文本显示为在线无限发布的东西。我们希望看到它写作,就像当您在屏幕上并且您正在写文本时,所以我们尝试了这个:

<!DOCTYPE html>
<head>

<script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

</head>

<body>
<div id="myTable"></div>

<script type="text/javascript">
function page_refresh() {
$.ajax({
url : 'getData.php',
type : 'GET',
success : function(data) {
$('#myTable').html(data);
},
error : function(request,error)
{
alert("Request error : "+JSON.stringify(request));
}
});
}

var period = 100; //NOTE: period is passed in milliseconds
setInterval(page_refresh, period);
</script>

</body>

对于 getData.php

<?php
$dbhandle = new PDO("mysql:host=localhost;dbname=writing", "root", "*********");//this connects to your mysql table
$sql = "SELECT text, id, date FROM table_02 ;"; //thise is your query, where you select what data you want from the table
$query = $dbhandle->prepare($sql);

if ($query->execute() == FALSE)
{ die("Query error: " . implode($query->errorInfo(), ' ')); } //this is a measure to close the connection if there is an error

echo('<table>');
echo('<tr><th>Test</th></tr>');
while ($row = $query->fetch()) {
echo('<tr>');
##echo('<td>'.$row ['id'].'</td>');
##echo('<td>'.$row ['date'].'</td>');
echo('<td>'.$row ['text'].'</td>');
echo('</tr>');
}
echo('</table>');
?>

var period = 100; //NOTE: period is passed in milliseconds
setInterval(page_refresh, period);

似乎在如此低的时间下效果不佳,看起来它只是用我们拥有的测试数据刷新整个页面。另外,由于某些原因,页面在显示数据之前会保持空白几秒钟。有没有更有趣的方法来做到这一点?

最佳答案

您是否了解使用 WebSockets 的知识?创建此技术是为了取代服务器的定期轮询以获取更新。服务器可以将更改推送到客户端,而不是从客户端进行轮询。您可以阅读更多相关信息 here

关于javascript - 平滑刷新显示来自不断更新的 MySQL 列的数据的 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44700569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com