每次从jQuery重新启动

每次从jQuery重新启动,jquery,each,Jquery,Each,我正在做一个jQuery幻灯片,准备预加载图像,然后将它们添加到另一个数组中。但是,我需要让幻灯片正常工作。我包括小提琴链接: 所以你可能会问怎么了,幻灯片效果如何?是的,但我需要使它最优化,因为现在它几乎崩溃,因为一些循环问题?我在foreach循环之后运行SlideShow()函数(我想),但这不起作用,因为我在开始时有很大的滞后。我想重新开始运行这个方法。我需要为此使用for循环吗 var img_load = new Array(); img_load[0]

我正在做一个jQuery幻灯片,准备预加载图像,然后将它们添加到另一个数组中。但是,我需要让幻灯片正常工作。我包括小提琴链接:

所以你可能会问怎么了,幻灯片效果如何?是的,但我需要使它最优化,因为现在它几乎崩溃,因为一些循环问题?我在foreach循环之后运行SlideShow()函数(我想),但这不起作用,因为我在开始时有很大的滞后。我想重新开始运行这个方法。我需要为此使用for循环吗

        var img_load = new Array();

     img_load[0] = 'http://img.youtube.com/vi/KobO2FZYMtA/mqdefault.jpg';
     img_load[1] = 'http://img.youtube.com/vi/6FjdbO-CGC4/mqdefault.jpg';
     img_load[2] = 'http://img.youtube.com/vi/h-_cN3_zGuI/mqdefault.jpg';
     img_load[3] = 'http://img.youtube.com/vi/rPf0CTptt8o/mqdefault.jpg';

        /**
         * Foreach loop
         */
             function SlideShow()
             {
                $.each(img_load, function(i, val) { 
                $("#sImage").animate({opacity: 0.0}, 1000);
                /**
                 * Queue function will place the event in queue
                 * Changing image src after the above animate function is completed
                 */
                $("#sImage").queue(function(){
                    $("#sImage").attr("src", val);
                    $("#sImage").dequeue();                
                });

                $("#sImage").attr("src", val).animate({opacity: 1.0}, 1000);   

                /**
                 * Queue function will place the event in queue
                 * Here, queue function is used to hold the changing image for 1 second display
                 */            
                $("#sImage").queue(function(){                 
                    setTimeout(function(){
                        $("#sImage").dequeue();
                    }, 1000);
                });
            });
            SlideShow(); // Not good for some reason. I want to loop from start
        }

我不得不说,对于旋转木马,我不会真的采用这种方法,但网上有数百万的教程,所以我不会就此问题进行讨论。 您的问题是在循环中启动整个函数,然后再次启动函数等

对您来说,最快的修复方法是检查数组长度,并仅在最后激发它

看到小提琴了吗

var img_load=新数组()

谢谢,我确实在最后尝试了if(img_load.length==I)进行一些测试,但我明白你的意思。非常感谢您的回复。关于旋转木马的错误方法。。。真是太遗憾了。我将查看一个教程,看看问题是什么。非常感谢。
 img_load[0] = 'http://img.youtube.com/vi/KobO2FZYMtA/mqdefault.jpg';
 img_load[1] = 'http://img.youtube.com/vi/6FjdbO-CGC4/mqdefault.jpg';
 img_load[2] = 'http://img.youtube.com/vi/h-_cN3_zGuI/mqdefault.jpg';
 img_load[3] = 'http://img.youtube.com/vi/rPf0CTptt8o/mqdefault.jpg';

    /**
     * Foreach loop
     */
         function SlideShow()
         {
            var count = 0;
            $.each(img_load, function(i, val) {
            $("#sImage").animate({opacity: 0.0}, 1000);
            /**
             * Queue function will place the event in queue
             * Changing image src after the above animate function is completed
             */
            $("#sImage").queue(function(){
                $("#sImage").attr("src", val);
                $("#sImage").dequeue();                
            });

            $("#sImage").attr("src", val).animate({opacity: 1.0}, 1000);   

            /**
             * Queue function will place the event in queue
             * Here, queue function is used to hold the changing image for 1 second display
             */            
            $("#sImage").queue(function(){                 
                setTimeout(function(){
                    $("#sImage").dequeue();
                    if((img_load.length-1) == count){
                        SlideShow();
                    }
                           count++;
                }, 1000);
            });
        });



    }

$(document).ready(function()
{
    SlideShow();
});​