PHP删除以前的循环数据
PHP delete previous loop data
所以我的网站上有一个"显示最近的"图片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的幻灯片插件。有很多选择,我最喜欢的是骑自行车。
- 如何在Angular.js中循环动态添加Fields并获取数据并将其发送到服务器
- 如何使用React JS中的循环,根据条件渲染或不渲染表数据
- 在Jade中循环并显示来自Mongodb的数据
- 如何无限循环数据集字符串
- 我如何在AJAX中循环数据并在其中存储特定值's各自的tr td
- 循环数据表行
- 简单的Ajax请求,在React.js中循环数据
- PHP删除以前的循环数据
- 使来自 GetJson 数组的循环数据成为链接
- 如何在javascript中循环数据
- 如何通过 AJAX 将循环数据从 html 页面传递到 php 页面
- 无法从循环数据属性的内容中获取索引的值
- 将php循环数据添加到js数组中
- 确定JavaScript中循环数据结构相等的算法
- 同步实例化模式——将JSON扁平化为实际的循环数据结构
- 我可以用JSON或类似的东西保存循环数据结构吗?
- 如何循环数据并设置它
- Javascript Highcharts:系列数据标签格式化程序函数,用于循环数据,而不是格式化数组中的所有数据
- a={prop:a}vs a.prop=a:为什么第一个不是循环数据结构
- 通过 AJAX 数据传递循环数据