我正在尝试使用Javascript和PHP实时更新数据库中的值。虽然值在数据库中不断更新,但除非我手动刷新页面,否则它不会在屏幕上刷新。有什么指针吗?
div class="panel-body" id = "tweetCounter">
<script>
//Javascript function that calls the PHP function
function tweetCounter()
{
var getTweetCount = <?php updateTweetCount();?>;
document.getElementById('tweetCounter').innerHTML = getTweetCount;
}
tweetCounter();
//Call function every 30 seconds
setInterval(function(){
tweetCounter();
}, 30000);
</script>
<!--PHP function to fetch details from DB -->
<?php
function updateTweetCount()
{
include('dbconnect.php');
$sql = "SELECT COUNT(tweetId) as tweetcount from brexittweets";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["tweetcount"];
}
} else {
echo "0 results";
}
}
?>
</div>