PHP删除以前的循环数据
所以我在我的网站上有一个“show recent”pictures div,我想每20秒刷新一次内容,以显示一张新图片。问题是,我当前的ajax调用会立即刷新自己,而不是在20秒之后,并且刷新时不会删除以前的数据,因此它会列出所有1000多张图片 以下是我的ajax调用:PHP删除以前的循环数据,php,javascript,ajax,Php,Javascript,Ajax,所以我在我的网站上有一个“show recent”pictures div,我想每20秒刷新一次内容,以显示一张新图片。问题是,我当前的ajax调用会立即刷新自己,而不是在20秒之后,并且刷新时不会删除以前的数据,因此它会列出所有1000多张图片 以下是我的ajax调用: $(window).load(function(){ var timer = 0; for (x =0; x<=20; x++) { timer++; if(tim
$(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;
}
}
}
$(窗口).load(函数(){
var定时器=0;
对于(x=0;xJavascript不会每秒运行一次“+”操作符-它将以非常快的速度运行二十次。您应该使用setInterval函数让js仅每隔20秒调用一次:
更改您的JS(未测试!!!):
编辑:您的PHP应该是这样的:
$imgdir = '../img/blog/img/amator/Amator_thumb/';
$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">';
}
}
$imgdir='../img/blog/img/amator/amator_thumb/;
$i=0;
$dimg=opendir($imgdir);//打开目录
而($imgfile=readdir($dimg))
{
if(在数组中(strtolower(substr($imgfile,-3)),$allowed_类型)或
在数组中(strtolower(substr($imgfile,-4)),$allowed\u类型)
/*如果文件是图像,请将其添加到数组中*/
{$a_img[]=$imgfile;}
如果($imgfile!=“&&&$imgfile!=”)
{
$imgaray[$i]=$imgfile;
$i++;
}
}
closedir($imgdir);
$totimg=count($a\u img);//所有图像的总数..img\u con
对于($x=$page*1;$x<$totimg&&$x<($page+1)*1;$x++){
$rand=rand(0,计数($imgarray)-1);
如果($rand>=0)
{
回声';
}
}
我根本没有测试过这个,但总体思路应该是这样的。javascript使用setInterval每隔20秒进行一次AJAX调用,然后PHP立即响应一些图像。这取决于您如何获得所需的图像
需要注意的是:不要使用for循环来计时!这就是所谓的CPU限制,除非你真的知道自己在做什么,否则不应该这样做
祝你好运!正如埃蒂所提到的,你的代码运行速度非常快,达到了20倍,因此它基本上一次发送20个请求。如果你能将递归函数设置为超时20秒
例:
PHP端不需要任何与计时器相关的东西。请注意,您可以在setTimeout()外部或内部运行ajax调用-这取决于您在第一个循环中要执行的操作(调用函数,然后等待20秒再进行ajax调用,或者每20秒进行一次ajax调用,第一次调用在调用函数时立即发生)
更好的选择是在javascript/jquery中使用幻灯片插件。有很多选择,我最喜欢的是。这不会在20次之后停止,这留给读者作为练习:p…并去掉PHP中与计时有关的所有代码。它似乎不起作用。20分钟后不会更新内容。@Erty Thewhile loop设置为while x@trisztann:为什么您希望用户在显示下一张图像之前等待20分钟?我猜您的意思是“秒”?
$(window).load(function(){
setInterval(function(){
//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 = function(){
if(xmlHttpRequest.readyState == 4){
document.getElementById('friss_kepek').innerHTML = xmlHttpRequest.responseText;
}
};
//Send the Ajax request to the server with the GET data
xmlHttpRequest.send(null);
}, 20000); //Run every 20000ms
}
$imgdir = '../img/blog/img/amator/Amator_thumb/';
$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">';
}
}
function updatePic(counter){
setTimeout(function(){
//do ajax call and you can use counter to determine what picture to return
updatePic(counter++);
}, 20000);
}