简体   繁体   English

jQuery无限动画导致调用堆栈错误

[英]jQuery infinite animation causing call stack error

I'm using the latest versions of jQuery and jQuery color. 我正在使用最新版本的jQuery和jQuery color。 I am trying to use animations to change the colors of my container infinitely. 我正在尝试使用动画来无限地更改容器的颜色。 But when I run the following code, I get the error: "Uncaught RangeError: Maximum call stack size exceeded". 但是,当我运行以下代码时,出现错误:“未捕获的RangeError:超出最大调用堆栈大小”。 The animation loop is working correctly, but it blocks the rest of my jQuery code from executing. 动画循环工作正常,但是它阻止了我其余的jQuery代码执行。 I don't know where the stack overflow is coming from..! 我不知道堆栈溢出从哪里来。

  let $container = $("#container");
  let colours = ["56, 68, 97", "97, 56, 80", "42, 74, 53", "104, 66, 44"];

  (function colourAnimation() {
    colours.forEach((colour) => {
      $container.animate({"color": "rgb(" + colour + ")",
                          "background-color": "rgba(" + colour + ", 0.2)",
                          "border-color": "rgba(" + colour + ", 0.7)",
                        }, 2500);
    });
    $container.animate({}, 0, "", colourAnimation);
  })()

Your function colourAnimation() ends with a call to a method animate , whose fourth parameter was passed colourAnimation . 您的function colourAnimation()以对animate方法的调用结束,该方法的第四个参数已传递colourAnimation
The docs @ https://api.jquery.com/animate/ indicate that that 4th argument is docs @ https://api.jquery.com/animate/表示第四个参数是

A function to call once the animation is complete, called once per matched element. 动画完成后调用的函数,每个匹配元素调用一次。

Of course the code would recurse infinitely ? 当然,代码会无限递归吗?

If you want your code to be run infinitely, just use setInterval(function, time) where time is in milliseconds. 如果您希望代码无限运行,只需使用setInterval(function, time) (时间以毫秒为单位)。 A better explanation can be found here 可以在这里找到更好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM