我定义了一个函数并将其称为递归函数,但它不起作用 这是代码
$(document).ready(function () {
//if i remove the slider call here it doesn't even run the function slider
slider();
function slider() {
$("#img2").fadeOut(3000, function () {
$("#img2").fadeIn(3000);
});
//recursive call
slider();
}
});
最佳答案
它正在工作,但您在 fadeOut
完成之前再次调用 slider()
。将递归调用粘贴在回调中:
function slider() {
$("#img2").fadeOut(3000, function () {
$("#img2").fadeIn(3000, function() {
//As Kristof Feys pointed out, you probably want to wait for the
//fade in to complete, then call the method.
slider();
});
});
}
还有一个演示:http://jsfiddle.net/9k7e3/
关于javascript - JavaScript 中的递归调用不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19791688/