Javascript “无限循环原因”;超过最大调用堆栈大小;错误
我试图得到一个无限循环,但过了一段时间,我得到了一个“超过最大调用堆栈大小”错误。现在我知道这是由inifite循环引起的,它本身是递归的,但我不明白为什么我的代码不正确,因为我想要那个无限循环 这是我用来调用“slide”函数的代码:Javascript “无限循环原因”;超过最大调用堆栈大小;错误,javascript,jquery,plugins,Javascript,Jquery,Plugins,我试图得到一个无限循环,但过了一段时间,我得到了一个“超过最大调用堆栈大小”错误。现在我知道这是由inifite循环引起的,它本身是递归的,但我不明白为什么我的代码不正确,因为我想要那个无限循环 这是我用来调用“slide”函数的代码: // The plugin usage => "Rock and Roll" function activate() { // Check or we need to wait, when a user hovers the main elemen
// 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毫秒后(通过示例)调用函数activate以转到下一张幻灯片。但正如我之前所说,它会导致“超出最大调用堆栈大小”错误。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
为什么它是无限的
setTimeout
只执行一次函数,我看不到任何递归activate
调用。幻灯片做什么?我看不出无限循环在哪里。你能提供幻灯片功能吗。setTimeout不会导致无限重复,setInterval会。这也不会导致这个错误。[更新以解决打字错误]@Lorefnon你是说设置间隔吗?@JosephDeTeamer是的。抱歉输入错误。@两者:我已经添加了幻灯片功能。我以前使用过setInterval函数,但这会引发相同的错误(可能会再次检查)。