如何在网页上显示随机图像 2 分钟,然后更改图像


How can I display a random image on a web-page for 2 minutes, then change images?

>我有一个小的php脚本,它应该显示文件夹中的随机图像,然后每2分钟更改一次图像。 我遇到的问题是,我们经常收到一个错误:

NOT FOUND

ERROR: could not connect to http://somedomain.com/TV/imgSlider.php

这是我用来随机选择图像(从名为 quotes 的文件夹中(进行显示的脚本,并每 120 秒刷新一次页面(使用元刷新(:

<?php
    $dirContents = scandir('quotes');
    // Unset the ".." and "." that are included in the array returned by scandir()
    unset($dirContents[0]);
    unset($dirContents[1]);
?>
<html>
    <body bgcolor="black">
    <?php
        // Grab random index for the array then grab the image name and push it into
        // the <img> tag.
        $randImgIndex = array_rand($dirContents);
        $randImg = $dirContents[$randImgIndex];
        echo "<img src='"quotes/$randImg'" style='"width:100%;height:100%'">";
    ?>
    </body>
<meta http-equiv="refresh" content="120">
</html>

是否有原因导致此脚本会引发这种类型的错误(或者为什么脚本可能无法访问(?我能做些什么来防止此错误? 还有其他方法可能会有所帮助吗?

谢谢!

如果您有兴趣尝试使用 Jquery,我个人认为这是一种更干净的方法,然后 php 用于此过程,然后尝试如下操作:

 <script> 
  function changeImg(min, max) { // create the function for changing the images
        var noi = max - min; // number of images
        var numRand = Math.floor(Math.random() * noi) + min; // randomized number
        $("#banner").find("img").attr('src','pages/gallery/PhotoWall/images/' + ""+ numRand +"" + '.jpg'); // set a new image
    }
    $(function() { // Waiting for the DOM ready
        setInterval(function(){ // create an interval (loop)
        changeImg(101, 120); // the function with paramteters
        },1000); // the interval in millisecondes --> 1000 = 1 second
    });
</script>

来源:https://forum.jquery.com/topic/getting-a-random-picture-to-display