所以我在我的网站上有一个“显示最近的”图片 div,我会每 20 秒刷新一次内容,以显示一张新图片。问题是我当前的 ajax 调用会立即刷新,而不是在 20 秒后刷新,并且刷新时不会删除以前的数据,因此它列出了所有 1000 多张图片。
这是我的ajax调用:
$(window).load(function(){
var timer = 0;
for (x =0; x<=20; x++)
{
timer++;
if(timer == 20 || x == 20)
{
//create XMLHttpRequest object
xmlHttpRequest = (window.XMLHttpRequest) ?
new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
//If the browser doesn't support Ajax, exit now
if (xmlHttpRequest == null)
return;
//Initiate the XMLHttpRequest object
xmlHttpRequest.open("GET", "../php/rotalas.php", true);
//Setup the callback function
xmlHttpRequest.onreadystatechange = updt_pictures;
//Send the Ajax request to the server with the GET data
xmlHttpRequest.send(null);
}
function updt_pictures()
{
if(xmlHttpRequest.readyState == 4)
{
document.getElementById('friss_kepek').innerHTML = xmlHttpRequest.responseText;
}
}
}
这是列出新文件的调用php
<?php
$timer = 0;
for($x = 0; $x < 20; $x++)
{
if($timer == 20 OR $x==20)
{
$timer = 0;
$x= 0;
}
}
$x = 1;
while($x >=$timer)
{
$imgdir = '../img/blog/img/amator/Amator_thumb/'; //Pick your folder .. images
$i=0;
$dimg = opendir($imgdir);//Open directory
while($imgfile = readdir($dimg))
{
if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
/*If the file is an image add it to the array*/
{$a_img[] = $imgfile;}
if ($imgfile != "." && $imgfile!="..")
{
$imgarray[$i]=$imgfile;
$i++;
}
closedir($imgdir);
$totimg = count($a_img); //The total count of all the images .. img_coun
for($x=$page*1; $x < $totimg && $x < ($page+1)*1; $x++){
$rand=rand(0,count($imgarray)-1);
if($rand >= 0)
{
echo '<img class="kep_listaz" src="../img/blog/img/amator/Amator_thumb/'.$imgarray[$rand].'" width="160" height="140">';
}}
我尝试在循环结束时使用 sleep(20),但它根本没有刷新。
谢谢!