我通过setinterval()
函数触发多个ajax请求,这些请求从另一个页面带来一些信息,但这些请求从两个请求中提供相同的信息。
这里是Javascript代码
function views()
{
setInterval(function(){var xmllhttp
//alert("views")
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("latestviews").innerHTML=xmlhttp.responseText
}
}
xmlhttp.open("GET","latestviews.php")
xmlhttp.send()},5000);
}
function recentposts()
{
setInterval(function()
{
var xmllhttp
//alert("recent")
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest()
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("latest").innerHTML=xmlhttp.responseText
}
}
xmlhttp.open("GET","recent.php")
xmlhttp.send()},5000);
}
这是html代码
<body onload="views(),recentposts()">
<div class="latest" id="latest">
<span class="latestin">
<label class="label"><i>Recent Post</i></label>
</span>
<?php
$con1 = mysqli_connect('127.0.0.1','root','root','databasetry');
$result=mysqli_query($con1,"SELECT articleid,title FROM article order by articleid desc LIMIT 6");
$divid=0;
while($row = mysqli_fetch_array($result))
{
$id=0;
$id=$row['articleid'];
echo"<div class='recent' onclick='ajaxinput($id)' id=$id style='cursor:pointer;'>";
echo $row['title']."<br>";
echo"</div>";
}
mysqli_close($con1);
?>
</div>
<div class="latestviews" id="latestviews">
<span class="latestin">
<label class="label"><i>Top viewed Post</i></label>
</span>
<?php
$con1 = mysqli_connect('127.0.0.1','root','root','databasetry');
$result=mysqli_query($con1,"SELECT articleid,title FROM article order by views desc LIMIT 6");
$divid=0;
while($row = mysqli_fetch_array($result))
{
$idd=0;
$idd=$row['articleid'];
echo"<div class='recent' onclick='ajaxinput($idd)' id=$idd style='cursor:pointer;'>";
echo $row['title']."<br>";
echo"</div>";
}
mysqli_close($con1);
?>
</div>
</div>
</body>
答案 0 :(得分:0)
您应该为AJAX URL添加时间戳,以防止浏览器缓存结果。
xmlhttp.open("GET","latestviews.php" + "?time=" + new Date().getTime());