调试过多的递归
我想知道是否有人知道寻找解决此问题的好地方。我一直在研究一个简单的动画,它获取无序列表中的项目并通过它们淡入和淡出。
<div id="testimonialTitle">Customer Testimonials</div>
<div id="testimonialQuote"></div>
<div id="testimonialAuthor"></div>
<!-- HIDDEN DIVS put your testimonials here-->
<div id="testimonialList" style="display:none">
<ul>
<li>
<div class="quote">"Integer posuere erat a ante venenatis d." </div>
<div class="author">-Bob the Builder</div>
</li>
<li>
<div class="quote">"Maecenas sed diam eget risus vari." </div>
<div class="author">-Mr. T</div>
</li>
和 js
$(document).ready(function(){
animateDiv();
});
var myList = '#testimonialList ul li';
var speed = 'slow';
var duration = 8000;
function animateDiv() {
$(myList).each(function(index) {
var quote = $(this).find('.quote').text();
var author = $(this).find('.author').text()
setTimeout(function() {
$('#testimonialQuote, #testimonialAuthor').fadeOut(speed, function() {
$('#testimonialAuthor').text(author).fadeIn(speed);
$('#testimonialQuote').text(quote).fadeIn(speed, function () {
if (($(myList).length) == index + 1) {
setTimeout(function() {
animateDiv();
}, duration)
}
});
});
}, index * duration);
});
}
由于某种原因,动画运行良好几次,然后,我松开引号,作者以大约 1 秒的间隔闪烁。 Firebug 告诉我“递归太多”。我知道我已经创建了一个无限循环,但当然我想要的是一个不断循环的动画。我无法理解如何调试此类错误以及在哪里可以找到有关此类事情的一些好的信息。
有什么建议吗?
I was wondering if anyone knows of good place to look for the solution to this problem. I've been working on a simple animation that takes items in an unordered list and fades in and out through them.
<div id="testimonialTitle">Customer Testimonials</div>
<div id="testimonialQuote"></div>
<div id="testimonialAuthor"></div>
<!-- HIDDEN DIVS put your testimonials here-->
<div id="testimonialList" style="display:none">
<ul>
<li>
<div class="quote">"Integer posuere erat a ante venenatis d." </div>
<div class="author">-Bob the Builder</div>
</li>
<li>
<div class="quote">"Maecenas sed diam eget risus vari." </div>
<div class="author">-Mr. T</div>
</li>
and js
$(document).ready(function(){
animateDiv();
});
var myList = '#testimonialList ul li';
var speed = 'slow';
var duration = 8000;
function animateDiv() {
$(myList).each(function(index) {
var quote = $(this).find('.quote').text();
var author = $(this).find('.author').text()
setTimeout(function() {
$('#testimonialQuote, #testimonialAuthor').fadeOut(speed, function() {
$('#testimonialAuthor').text(author).fadeIn(speed);
$('#testimonialQuote').text(quote).fadeIn(speed, function () {
if (($(myList).length) == index + 1) {
setTimeout(function() {
animateDiv();
}, duration)
}
});
});
}, index * duration);
});
}
for some reason the animation runs fine a couple times then, I loose the quotes and the authors flash on approximately 1 second intervals. Firebug tells me "too much recursion". I know I've created an infinite loop but of course what I wanted was an animation that would continually loop. I'm having trouble understanding how to debug this sort of error and where to find some good info on this sort of thing.
Any Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是个傻瓜,我已经回答了我自己的问题,但对于那些可以使用这里解决方案的人来说。
我应该使用 setInterval 再次调用我的函数,而不是让我的函数调用本身。现在也干净了。我只是在开始时调用 animateDiv() 一次,然后根据列表中的元素数量乘以我设置的持续时间设置调用 animateDiv 的间隔。
I'm a fool, I've answered my own question, but for those of you that could use the solution here it is.
I should have been using setInterval to call my function again instead of calling having my function call itself. Cleaner too now. I just called animateDiv() once at the beginning, then set the interval to call animateDiv depending on the number of elements in my list multiplied by the duration that i set.