触发多个ajax请求,这些都显示相同的结果
firing multiple ajax request and these are showing the same result?
我通过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>
您应该在AJAX url中添加时间戳,以防止浏览器缓存结果。
xmlhttp.open("GET","latestviews.php" + "?time=" + new Date().getTime());
相关文章:
- 当查询不在displayField中时,引导Ajax Typeahead不显示结果
- JavaScript循环无法正确计算/显示结果
- 将循环中的两个文本框相乘,并在第三个文本框上显示结果
- BMI计算器以Javascript显示结果
- 在表中显示结果
- onchange选择get value并执行查询,然后在同一页面上显示结果
- 搜索XML文件并使用javascript显示结果
- Moment.js不显示结果
- 使用jQuery延迟表单输入,并在延迟后在同一页面上显示结果
- 防止 PHP 在新页面中显示结果
- jquery jqgrid 不显示结果,具体取决于 JSON 对象中的参数
- Angular-如何显示结果,仅在未选中时显示
- backbone.js视图没有't由于异步获取而显示结果;t渲染
- jqueryajax帖子将我发送到操作页面,并且不会显示结果数据
- 添加两个数字,并使用Javascript在文本框中显示结果
- 接受用户输入,与JSON数据匹配,并以javascript显示结果
- 自动完成 JQuery 不显示结果
- 控制台.log或显示结果的函数
- 提交表单,使用脚本处理信息,并在同一页面上显示结果
- AJAX 实时搜索仅当结果以相同的第一个字母开头时才显示结果