Jquery太多递归错误 [英] Jquery Too Much Recursion Error

查看:352
本文介绍了Jquery太多递归错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能帮助我。



我有这个代码:

 < script> 
$(document).ready(function(){
spectrum();
function spectrum(){
$('#bottom-menu ul li.colored a') .animate({color:'#E7294F'},16000);
spectrum2();
}
function spectrum2(){
$('#bottom-menu ul li .colored a')。animate({color:'#3D423C'},16000);
spectrum();
}
}
< / script>

它的工作,但是当我看着firebug它说有一个Too Much Recursion错误。 >

我希望有人能告诉我为什么。



谢谢!



当页面加载时,你要让它运行函数

code> spectrum()。它运行这个函数,然后被告知要运行 spectrum2()函数。当它完成 spectrum2(),你就让它再次运行 spectrum()再次运行 spectrum2()。看到模式?



函数调用自身(或两个函数重复调用其他函数)的过程称为< a href =http://en.wikipedia.org/wiki/Recursion =nofollow noreferrer>递归,但通常递归最终以某种方式终止。你的永远不会终止,所以FireBug说等一下,这个脚本永远不会结束,我最好抛出一个错误!



这可能不是什么你试图实现,这个修复很可能很简单。如果你可以尝试并解释你想达到的目标,也许我们可以帮你写正确的代码?


I hope someone could help me.

I have this code:

<script>
$(document).ready(function() {
 spectrum();
 function spectrum(){
    $('#bottom-menu ul li.colored a').animate( { color: '#E7294F' }, 16000);
    spectrum2();
 }
 function spectrum2(){
    $('#bottom-menu ul li.colored a').animate( { color: '#3D423C' }, 16000);
    spectrum();
 }
});
</script>

it's working but when I look at firebug it says that there's a Too Much Recursion error.

I hope someone can tell me why.

Thanks!

解决方案

The problem is that your script never stops executing.

When the page loads, you tell it to run the function spectrum(). It runs this function, and is then told to run the function spectrum2(), which it does. When it finishes spectrum2(), you tell it to run spectrum() again, and when it's done that it has to run spectrum2() yet again.. see the pattern? Your poor script is stuck executing those two functions over and over, forever!

The process of a function calling itself (or two functions calling each-other repeatedly) is called recursion, but normally the recursion ultimately terminates in some way. Yours never terminates, so FireBug says "Wait a minute, this script is never going to end, I'd better throw an error!"

This probably isn't what you're trying to achieve, and the fix is most likely simple. If you could try and explain what you're trying to achieve, maybe we can help you write the proper code?

这篇关于Jquery太多递归错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆