我的 jQuery 滑块运行良好,但存在错误,它有时不会在下次或上一次单击时显示图像

my jquery slider is running well but there is bug that it sometimes does'nt shows images on next or prev click

本文关键字:下次 上一次 图像 显示图 显示 单击 运行 jQuery 我的 错误 存在      更新时间:2023-09-26

找到了播放和停止滑块的解决方案。但是现在问题出在我的下一个和上一个链接上一个链接上,这些链接有时不显示任何图像。我的代码在http://jsfiddle.net/yogesh84/ftkLd/12/

var slides;
        var cnt;
        var amount;
        var i;
        var x;
        var timer;
        slides = jQuery('#my_slider').children();
        cnt = jQuery('#counter');
        amount = slides.length;
        i=amount;
 cnt.text(i+' / '+amount);
        function run_prev() {
          jQuery(slides[i]).fadeOut(1000);
            i--;
            if (i <= 0) i = amount;
            jQuery(slides[i]).fadeIn(1000);
            // updating counter
            cnt.text(i+' / '+amount);
        }
        x=0;
        function run_next() {
            // hiding previous image and showing next
            jQuery(slides[x]).fadeOut(1000);
            x++;
            if (x >= amount) x = 0;
            jQuery(slides[x]).fadeIn(1000);
            cnt.text(x+1+' / '+amount);
        }
        /***********start and stop functions***************/
        function run() {
        // hiding previous image and showing next
        jQuery(slides[x]).fadeOut(1000);
        x++;
        if (x >= amount) x = 0;
        jQuery(slides[x]).fadeIn(1000);
        timer = setTimeout(run,2000);
        }
        function MySlider() {
             timer = setTimeout(run,2000);
        }
        function stoper() {
              clearTimeout(timer);
        }
/***********end start and stop functions***************/

        function slide_show(){
            var timer;
                if(jQuery('#slide_show').html()=='Play Slideshow')
                {
                    jQuery('#slide_show').html('Stop Slideshow');
                     MySlider();
                }
                else
                { 
                   jQuery('#slide_show').html('Play Slideshow');
                   stoper()
                }
            }

// custom initialization
    jQuery('#prev2').on("click",run_prev);
    jQuery('#next2').on("click",run_next);
    jQuery('#slide_show').on("click",slide_show);

我想你忘了设置timer

您在顶部声明:timer但它永远不会被填充。

所以我认为你应该这样做:

timer =  setTimeout( run, inetrval );

也把函数放在 Run() 之外

function run() {
}
if ( inetrval > 0 ) {
    alert( inetrval );
    run();
    timer = SetTimeOut( run, inetrval )
}