0

我试图获得一个无限循环,但过了一会儿我得到一个“超出最大调用堆栈大小”的错误。现在我知道这是由一个无限循环引起的,它本身是递归的,但我不知道为什么我的代码不正确,因为我想要那个无限循环。

这是我用来调用“幻灯片”函数的代码:

// The plugin usage => "Rock and Roll"
function activate() {
    // Check or we need to wait, when a user hovers the main element
    if (hoverState === false) {
        // And "Rock and Roll"
        if (opt.nav === false) {
            slide(opt.effect);
        } else {
            // The navigation is used
            $(opt.navArea + ' a').click(function () {
                if ($(this).attr('id') === opt.next) {
                    // Slide to the next slide
                    slide(opt.effect, 'right');
                } else if ($(this).attr('id') === opt.prev) {
                    slide(opt.effect, 'left');
                }
            }, function () {
                slide(opt.effect);
            });
        }
    }
}
// Activate the plugin after the pause has ended
setTimeout(activate, opt.pause + opt.speed);

我的代码中有一些额外的选项,但是,我想要做的是(例如)3000 毫秒后调用函数激活以转到下一张幻灯片。但就像我之前说的,它会导致“超出最大调用堆栈大小”错误。opt.nav 的当前状态为 false。

滑动功能

    // Slide the content
    function slide( effect, dir ) {
        // The direction
        dir = typeof dir !== 'undefined' ? dir : 'right';

        // Define the current slide
        var currSlide = $('li.active');

        // Define the next slide
        var nextSlide;
        if( dir === 'right' ) {
            nextSlide = currSlide.index() + 1;

            // Check or the next slide is avaiable
            if( nextSlide > ( elListSize - 1) ) {
                nextSlide = 0;
            } 
        } else if( dir === 'left' ) {
            nextSlide = currSlide.index() - 1;

            // Check or next slide is avaiable
            if( nextSlide === 0) {
                nextSlide = elListSize - 1;
            }
        }

        // The fade function
        if( effect === 'fade' )  {
            // Fade the current slide out
            currSlide.fadeOut(opt.speed, function() {
                // Remove the active class from the current slide
                currSlide.removeClass('active');

                // Fade the next slide in an give it the class active
                elListItem.eq(nextSlide).fadeIn(opt.speed, function() {
                    elListItem.eq(nextSlide).addClass('active');
                });
            });                 
        }
    } // End of the slide function
4

0 回答 0