PHP删除以前的循环数据

PHP delete previous loop data

本文关键字:循环 数据 删除 PHP      更新时间:2023-09-26

所以我的网站上有一个"显示最近的"图片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),但它根本没有刷新。

谢谢!

Javascript不会每秒运行一次"++"运算符,它会运行二十次,非常快。您应该使用setInterval函数使js只每20秒调用一次:

http://www.w3schools.com/jsref/met_win_setinterval.asp

更改你的JS(未测试!!):

$(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
}

编辑:你的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">';
    }
}

我还没有对此进行测试,但总体思路应该是存在的。javascript使用setInterval每隔20秒进行一次AJAX调用,然后PHP立即用一些图像进行响应。这取决于你想办法得到你想要的图像。

需要注意的是:不要使用for循环进行计时这就是所谓的CPU限制,除非你真的知道自己在做什么,否则不应该这样做。

祝你好运!

正如Erty所提到的,您的代码运行速度非常快20倍,因此它基本上一次发送20个请求。如果您可以设置递归函数的超时时间为20秒。

例如:

function updatePic(counter){
  setTimeout(function(){
    //do ajax call and you can use counter to determine what picture to return
    updatePic(counter++);
  }, 20000);
}

PHP方面不需要任何与计时器相关的东西。请注意,您可以在setTimeout()外部或内部运行ajax调用,这取决于您想在第一个循环中执行什么(调用函数,然后等待20秒再进行ajax调用,或者每20秒进行一次ajax调用,第一次调用在调用函数时立即发生)。

更好的选择是使用javascript/jquery的幻灯片插件。有很多选择,我最喜欢的是骑自行车。