douyi2664 2015-12-08 07:13
浏览 59
已采纳

返回唯一索引ajax

I have code which change photos every x seconds. My jQuery code:

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>

And code of my 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]);

?>

Records aren't unique (sometimes it shows 2 the same images). Is it possible to change it?

This is my site: http://patwoj.hekko24.pl/rotator/

</div>
  • 写回答

3条回答 默认 最新

  • duanji5569 2015-12-08 07:35
    关注

    You better control the randomness of your images from within the server and gell all 4 of them in a single ajax call:

    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);
        }
      });
    }
    

    And your 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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题