Jquery 太多递归错误

发布于 2024-09-05 17:02:01 字数 446 浏览 5 评论 0原文

我希望有人能帮助我。

我有这段代码:

<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>

它正在工作,但是当我查看萤火虫时,它说存在太多递归错误。

我希望有人能告诉我原因。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘸点软妹酱 2024-09-12 17:02:01

问题是你的脚本永远不会停止执行。

当页面加载时,您告诉它运行函数spectrum()。它运行这个函数,然后被告知运行函数spectrum2(),它确实这样做了。当它完成 spectrum2() 时,您告诉它再次运行 spectrum(),完成后它必须运行 spectrum2()再次..看到模式了吗?你可怜的脚本被困在一遍又一遍地执行这两个函数,永远!

一个函数调用自身(或者两个函数重复调用对方)的过程称为递归 ,但通常递归最终会以某种方式终止。你的脚本永远不会终止,所以 FireBug 会说“等一下,这个脚本永远不会结束,我最好抛出一个错误!”

这可能不是您想要实现的目标,并且修复很可能很简单。如果您可以尝试解释您想要实现的目标,也许我们可以帮助您编写正确的代码?

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?

你的心境我的脸 2024-09-12 17:02:01

你有一个明显的无限递归。 spectrum() 调用spectrum2(),而spectrum2() 又调用spectrum();您没有终止条件。您需要添加一个条件来终止该递归。也许您想实现以下目标。如果您让我们知道您想要实现的目标,那么您将得到解决方案。

<script>
$(document).ready(function() {
 spectrum();

 function spectrum(toEnd){
    $('#bottom-menu ul li.colored a').animate( { color: '#E7294F' }, 16000, function(){
     if(!toEnd)
        spectrum2(true);
     });
 }
 function spectrum2(toEnd){
    $('#bottom-menu ul li.colored a').animate( { color: '#3D423C' }, 16000, function(){

     if(!toEnd)
        spectrum(true);
    });
 }
});
</script>

You have a clear endless recursion. specturm() calls spectrum2() which in turn calls spectrum(); you do not have a condition for terminating. You need to add a condition to terminate that recursion. Perhaps you want to achieve the following. If you let us know what you are trying to achieve, then you will get a solution.

<script>
$(document).ready(function() {
 spectrum();

 function spectrum(toEnd){
    $('#bottom-menu ul li.colored a').animate( { color: '#E7294F' }, 16000, function(){
     if(!toEnd)
        spectrum2(true);
     });
 }
 function spectrum2(toEnd){
    $('#bottom-menu ul li.colored a').animate( { color: '#3D423C' }, 16000, function(){

     if(!toEnd)
        spectrum(true);
    });
 }
});
</script>
优雅的叶子 2024-09-12 17:02:01

使用setTimeout从堆栈中删除调用函数:

function spectrum(){
    $('#bottom-menu ul li.colored a').animate( { color: '#E7294F' }, 16000);
    setTimeout(function() {spectrum2();},100);
 }

这样,当您启动spectrum2时,spectrum就有机会完成。对spectrum2 做同样的事情。

use setTimeout to remove the calling functions from the stack:

function spectrum(){
    $('#bottom-menu ul li.colored a').animate( { color: '#E7294F' }, 16000);
    setTimeout(function() {spectrum2();},100);
 }

this way, spectrum has a chance to finish while you start spectrum2. Do same with spectrum2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文