gpt4 book ai didi

javascript - 在 jQuery 中使用回调函数无限递归

转载 作者:行者123 更新时间:2023-12-01 02:25:12 25 4
gpt4 key购买 nike

尝试使用 jQuery 并尝试制作一个旋转三个图像的小型幻灯片。这是我的 HTML:

<div id="slideShow">
<img src="images/slides/slide1.jpg" width="520" height="230" />
<img src="images/slides/slide2.jpg" width="520" height="230" />
<img src="images/slides/slide3.jpg" width="520" height="230" />
</div>

这是脚本:

$(function ()
{
var $slides = $('#slideShow img').hide(),
slideIndex = 0;

slideTransition = function ()
{
slideIndex++;
(slideIndex == $slides.length) ? slideIndex = 0: null;
$slides.eq(slideIndex).fadeIn(3000);
$slides.eq(slideIndex).fadeOut(3000,slideTransition);
}

$slides.eq(0).fadeIn(3000);
$slides.eq(0).fadeOut(3000, slideTransition);

});

这实际上工作得很好,但我的直觉告诉我,无限递归是一件坏事。关于如何做得更好有什么建议吗?

最佳答案

你所做的不是递归。当动画开始时,您的调用返回。然后,计时器事件处理程序调用您的方法。你的解决方案很好。

关于javascript - 在 jQuery 中使用回调函数无限递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5113640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com