运行下面的代码一段时间后,我收到“超出最大堆栈调用大小”错误。 setInterval()
应该能够无缝递归运行,所以我还不确定错误是由它还是代码的其他部分引起的。
function blinkCursor() {
$("#cursor").animate({opacity: 0}).animate({opacity: 1});
} setInterval(blinkCursor, 600);
这是代码的最后一部分。但我在没有上述部分的情况下对其进行了测试并且没有问题。无论如何,现在我不太确定。
function type() {
var firstSentence = "Human rights",
firstSentenceCounter = 0,
intervalID = setInterval(typeWithDelay, 150);
function typeWithDelay() {
$("#text").text(firstSentence.substring(0, firstSentenceCounter++));
if(firstSentenceCounter == firstSentence.length+1) {
clearInterval(intervalID);
}
}
} setTimeout(type, 1500);
最佳答案
animate()
默认持续时间为 400。
您在动画完成之前很久就不断调用函数,并不断添加到永不停止增长的动画队列中。
400+400 = 800 ...大于当前使用的 600 区间
更好的方法是使用最后一个动画回调来初始化下一轮而不是使用 setInterval
function blinkCursor() {
$("#cursor").animate({opacity: 0}).animate({opacity: 1}, blinkCursor );
}
blinkCursor(); // initialize once
关于javascript - setInterval() 导致最大堆栈调用大小超出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41783267/