【问题标题】:"too much recursion" in my rotating banner script我的旋转横幅脚本中的“递归过多”
【发布时间】:2011-01-19 16:14:37
【问题描述】:

当在init(); 中调用iKeyless.widget.Rotator.rotate(); 函数时,我的旋转横幅脚本抛出“递归过多”错误。我不知道为什么,init(); 只被调用一次,并且在第二次调用中有一个 defer() ......当页面加载它在抛出“太多递归”之前滞后

    return {
        init: function () {
            ui.container = $("#rotating-banner");
            ui.thumbs = $("#rotating-banner .tabs .tab");
            ui.banners = $("#rotating-banner .banner");
            ui.fadeBox = $(".rotate .bd .fade-box");

            ui.thumbs.each(function (idx, el) {
                $(el).click(function () {
                    paused = true;

                    iKeyless.widget.Rotator.rotate.show(idx);
                });
            });

            iKeyless.widget.Rotator.rotate();
        },
        show: function (idx) {
            ui.fadeBox.css("display", "block");
            ui.fadeBox.animate({
                opacity: 1
            },
            .125,
            function () {
                $('.active', $(ui.banners[idx]).addClass('active').parent()).removeClass('active');

                ui.fadeBox.animate({
                    opacity: 1
                },
                .125,
                function () {
                    ui.fadeBox.css("display", "none");
                });
            });
        },
        rotate: function () {
            if (!paused) {
                this.show(counter);

                counter = counter + 1;

                if (counter === ui.thumbs.length) {
                    counter = 0;
                }

                iKeyless.widget.Rotator.rotate().defer(5000);
            }
        }
    }

【问题讨论】:

  • Tagwise - 这是 Prototype 而不是 jQuery?

标签: javascript jquery


【解决方案1】:

我认为问题是你需要这样做

iKeyless.widget.Rotator.rotate.defer(5000);

而不是这个

iKeyless.widget.Rotator.rotate().defer(5000);

Source: 编写rotate() 时,执行函数rotate,然后执行defer。当您编写 rotate.defer() 时,您将获得函数 rotate,并使用函数上定义的方法 defer

另外,如果这是 jQuery,你是如何定义 defer 的?我不确定 jQuery 是否提供了延迟功能。如果这是原型,我认为 defer 不需要任何参数。也许你想要delay。延迟需要以秒为单位的时间,因此您希望(假设为 5 秒,而不是 5000 秒):

iKeyless.widget.Rotator.rotate.delay(5);

【讨论】:

    【解决方案2】:

    我不知道延迟,但我读到的是这样的:

    安排函数在解释器空闲时立即运行。

    但这并不意味着它实际上并没有这样做。函数 rotate 一次又一次地调用自己。不是线性的,而是在自身内部,所以你永远不会结束'top'调用旋转。

    这意味着无限递归,所以我对这个错误并不感到惊讶。

    【讨论】:

      【解决方案3】:

      我对@9​​87654321@ 不熟悉,但您在rotate 方法中调用rotate(),这是递归的来源。也许利用setTimeoutsetInterval 之类的方法可以解决您的问题?

      【讨论】:

        猜你喜欢
        • 2016-04-15
        • 1970-01-01
        • 1970-01-01
        • 2011-01-04
        • 1970-01-01
        • 1970-01-01
        • 2017-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多