jQuery - 未捕获的 RangeError:超出最大调用堆栈大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16694209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - Uncaught RangeError: Maximum call stack size exceeded
提问by Clive van Hilten
The following code (see Fiddle here) throws the stack overflow referred to in the question title. I'm trying to get a box shadow to display around a circular image in a pulse effect. Can anyone point out the recursion, please? I'm very much a Javascript novice and can't see it. Thank you.
以下代码(请参阅此处的 Fiddle)引发问题标题中提到的堆栈溢出。我正在尝试使框阴影以脉冲效果显示在圆形图像周围。任何人都可以指出递归吗?我是一个 Javascript 新手,看不到它。谢谢你。
HTML
HTML
<div id="pulseDiv">
<a href="#" id="advisers-css-image">
<div id="advisersDiv"><img src="http://ubuntuone.com/1djVfYlV62ORxB8gSSA4R4"></div>
</a>
</div>
CSS
CSS
.pulse { box-shadow: 0px 0px 4px 4px #AEA79F; }
Javascript
Javascript
function fadeIn() {
$('#pulseDiv').find('div.advisersDiv').delay(400).addClass("pulse");
fadeOut();
};
function fadeOut() {
$('#pulseDiv').find('div.advisersDiv').delay(400).removeClass("pulse");
fadeIn();
};
回答by PSL
Your calls are made recursively which pushes functions on to the stack infinitely that causes max call stack exceeded error due to recursive behavior. Instead try using setTimeout which is a callback.
您的调用是递归进行的,它将函数无限地推入堆栈,导致由于递归行为而导致最大调用堆栈超出错误。而是尝试使用 setTimeout 这是一个回调。
Also based on your markup your selector is wrong. it should be #advisersDiv
同样根据您的标记,您的选择器是错误的。它应该是#advisersDiv
Demo
演示
function fadeIn() {
$('#pulseDiv').find('div#advisersDiv').delay(400).addClass("pulse");
setTimeout(fadeOut,1); //<-- Provide any delay here
};
function fadeOut() {
$('#pulseDiv').find('div#advisersDiv').delay(400).removeClass("pulse");
setTimeout(fadeIn,1);//<-- Provide any delay here
};
fadeIn();
回答by Duan Walker
your fadeIn()
function calls the fadeOut()
function, which calls the fadeIn()
function again. the recursion is in the JS.
您的fadeIn()
函数调用该fadeOut()
函数,该fadeIn()
函数再次调用该函数。递归在 JS 中。