setinterval()减慢了网站的速度

时间:2016-06-15 14:22:04

标签: jquery

我有一个litle livescore页面,我使用setinterval函数在div中显示数据而不刷新。问题是网站需要更多的时间,而我认为,cpu ussage可以显示数据。我该怎么做才能提高代码性能?

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);
    });

1 个答案:

答案 0 :(得分:0)

我认为使用ajax是最好的方法,在你的setIntervall函数中调用一个脚本,用ajax返回每个span的HTML,在ajax调用成功你要用新的html刷新body

阅读jQuery API

上的文档