我有代码,每x秒更改一次照片。我的jQuery代码:
setInterval("rotator();",4000);
function rotator(){
var i = 0;
var zdjecie = '';
$('#rotate').html('');
while(i<4){
$.post('http://patwoj.hekko24.pl/rotator/content.php', function(data){
var wstaw='<li style="display:inline"><a href="images/'+data+'" class="highslide" title="" onclick="return hs.expand(this, config1 )"><img src="images/'+data+'" alt=""/> </a></li>';
$('#rotate').append(wstaw);
return data;
});
i++;
}
}
<ul id="rotate">
<li style="display:inline" id="rot1">
<a href="highslide/images/large/dyplom.jpg" class="highslide"
title=""
onclick="return hs.expand(this, config1 )">
<img src="http://www.akwarystyka-miedziowa.pl/highslide/images/thumbs/dyplom.jpg" alt=""/>
</a>
</li>
<li style="display:inline" id="rot2">
<a href="highslide/images/large/akw1.jpg" class="highslide"
title=""
onclick="return hs.expand(this, config1 )">
<img src="http://www.akwarystyka-miedziowa.pl/highslide/images/thumbs/akw1.jpg" alt=""/>
</a>
</li>
<li style="display:inline" id="rot3">
<a href="highslide/images/large/akw2.jpg" class="highslide"
title=""
onclick="return hs.expand(this, config1 )">
<img src="http://www.akwarystyka-miedziowa.pl/highslide/images/thumbs/akw2.jpg" alt=""/>
</a>
</li>
<li style="display:inline" id="rot4">
<a href="highslide/images/large/okon.jpg" class="highslide"
title=""
onclick="return hs.expand(this, config1 )">
<img src="http://www.akwarystyka-miedziowa.pl/highslide/images/thumbs/okon.jpg" alt=""/>
</a>
</li>
</ul>
和我的PHP 代码
<?php
$dir = 'images/';
$files = scandir($dir);
unset($files[0]);
unset($files[1]);
$max = max(array_keys($files));
$los = rand(2,$max);
print_r($files[$los]);
?>
记录并不是唯一的(有时它显示两个相同的图像)。有可能改变它吗?
这是我的网站:http://patwoj.hekko24.pl/rotator/
您可以更好地控制服务器内图像的随机性,并在一个ajax调用中将所有4个图像胶凝:
setInterval("rotator();",4000);
function rotator() {
$('#rotate').html('');
$.post('http://patwoj.hekko24.pl/rotator/content.php', function(data){
for(i=0;i<data.length;i++) {
var wstaw='<li style="display:inline"><a href="images/'+data[i]+'" class="highslide" title="" onclick="return hs.expand(this, config1 )"><img src="images/'+data[i]+'" alt=""/> </a></li>';
$('#rotate').append(wstaw);
}
});
}
和你的content.php
:
<?php
$dir = 'images/';
$files = scandir($dir);
unset($files[0]);
unset($files[1]);
$rand_imgs_json = json_encode(array_rand($files, 4));
header("Content-type: application/json");
echo $rand_imgs_json;
?>
对于图像目录中的每个文件名,您应该存储上次显示时间()的时间戳。
然后,对于每个请求,取那些保存了最旧时间戳的请求。一次多拿一些,然后随机挑选出来展示。存储选定文件的时间戳(),并显示它们。完成。
每次都会显示不同的文件,而且它们不会重复显示。
附言:你可以用很多方式存储时间戳,但如果你已经因为这个网站的其他原因使用了数据库连接,你最好选择有两个字段的数据库表-filename varchar UNIQUE,tm时间戳索引。然后只需通过tm DESC LIMIT向数据库请求订购数据,一次需要2倍的图像计数。您还必须正确管理该表的INSERT/UPDATE。
使用类似的东西
setInterval("rotator();",4000);
function rotator(){
var i = 0;
var zdjecie = []; // MODIFIED
$('#rotate').html('');
while(i<4){
$.post('http://patwoj.hekko24.pl/rotator/content.php', function(data){
// ADDED LINE BELOW
if(zdjecie.indexOf(data) < 0) {
var wstaw='<li style="display:inline"><a href="images/'+data+'" class="highslide" title="" onclick="return hs.expand(this, config1 )"><img src="images/'+data+'" alt=""/> </a></li>';
$('#rotate').append(wstaw);
zdjecie.push(data);
i++;
}
});
}
}
只有在名为zdjecie
的数组中不存在数据时,才能推送该数组中接收到的数据。当发生这种情况时,还要增加变量i
。因此,只有当有唯一的数据可用时,才会进行增量。最终,您将只获得唯一的数据。