0
我有一個litle livescore頁面,我使用setinterval函數在div中顯示數據而無需刷新。問題是,網站需要更多時間,我認爲,CPU的消息顯示數據。我能做些什麼來提高代碼性能?setinterval()減慢網站
data.php文件
<?php
$server = mysqli_connect("localhost", "root", "root");
$db = mysqli_select_db($server, "tenisapt");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "SELECT * FROM meciurilive WHERE id = '1'";
$result = mysqli_query($server, $sql);
$row = mysqli_fetch_assoc($result);
$sset1j1 = $row['Set1j1'];
$sset1j2 = $row['Set1j2'];
$sset2j1 = $row['Set2j1'];
$sset2j2 = $row['Set2j2'];
$sset3j1 = $row['Set3j1'];
$sset3j2 = $row['Set3j2'];
$sset4j1 = $row['Set4j1'];
$sset4j2 = $row['Set4j2'];
$sset5j1 = $row['Set5j1'];
$sset5j2 = $row['Set5j2'];
?>
<html>
<head>
</head>
<body>
<span id="j1s1"><?php echo $sset1j1;?></span>
<span id="j2s1"><?php echo $sset1j2;?></span>
<span id="j1s2"><?php echo $sset2j1;?></span>
<span id="j2s2"><?php echo $sset2j2;?></span>
<span id="j1s3"><?php echo $sset3j1;?></span>
<span id="j2s3"><?php echo $sset3j2;?></span>
<span id="j1s4"><?php echo $sset4j1;?></span>
<span id="j2s4"><?php echo $sset4j2;?></span>
<span id="j1s5"><?php echo $sset5j1;?></span>
<span id="j2s5"><?php echo $sset5j2;?></span>
</body>
</html>
index.php文件
$(document).ready(function() {
setInterval(function() {
$('#showj1s1').load('data.php #j1s1');
$('#showj2s1').load('data.php #j2s1');
$('#showj1s2').load('data.php #j1s2');
$('#showj2s2').load('data.php #j2s2');
$('#showj1s3').load('data.php #j1s3');
$('#showj2s3').load('data.php #j2s3');
$('#showj1s4').load('data.php #j1s4');
$('#showj2s4').load('data.php #j2s4');
$('#showj1s5').load('data.php #j1s5');
$('#showj2s5').load('data.php #j2s5')
}, 7500);
});
嘗試使用$ .ajax一次性獲取完整的'$ row'數組,而不是執行多次加載。 –
是的,就像上面提到的那樣,只有一次調用才能從data.php中獲取數據,而不是那麼多的調用。這肯定會減慢客戶機。 – Vickrant
和以前的答案一樣,只需要用Ajax調用你的PHP,然後在回到你的JS後處理顯示:)。此外,如果發生錯誤,你應該退出你的ajax,在這裏你只是迴應錯誤(爲了調試的目的,我假設?) – Julqas