Using SetTimeout and .each Jquery



我想将settimeout()函数与.each函数一起使用。基本上我想把每张照片都显示5秒钟,然后再显示下一张,但我只能看到最后一张。每个.执行并且在3秒钟内不停止。我该怎么做?我就是这样做的。

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
        $('#Images').find('li').each(function () {
            var img = this;
            setTimeout(function () {
                Start(img);
            }, 3000);
        });

    });
    function Start(img) {
        $('#slideshow').html(img);
    }
   </script>
   </head>
   <body>
   <form id="form1" runat="server">
<div id="slideshow">
</div>
<div style="display:none;">
<ul id="Images">
<li><img src="images/ajax-loader.gif" /></li>
<li><img src="images/Ajax Loader White.gif" /></li>
<li><img src="images/fancybox_sprite.png" /></li>
</ul>
</div>
</form>

试试这个:

    $('#Images').find('li').each(function (k,v) {
        var img = this;
        changeImg(img, k);
    });
    function changeImg(img, k) {
        setTimeout(function () {
            Start(img);
        }, 3000*(k+1));
    }

试试这个

var images = $('#Images').find('li');
var imageCount = images.length;
var counter = 0;
setTimeout(function(){
Start(images[counter++]);
if(counter>=imageCount)
    counter =0 ;
},3000);

最新更新