javascript - 为什么我的 jquery animate() 回调会导致溢出?递归

标签 javascript jquery recursion callback jquery-animate

我正在使用带有 animate() jquery 函数的递归回调。 然而,页面每次从一开始就会崩溃。

var goingDown = true;

function animateChevron() {
        if (goingDown) {
            goingDown = !goingDown;
            $('#chevron').animate({'opacity': 1}, 500, animateChevron);
        }
        else {
            goingDown = !goingDown;
            $('#chevron').animate({'opacity': 0.1}, 500, animateChevron);
        }
}

$(document).ready(function(){
    animateChevron();
});

谢谢

编辑:我希望它循环运行:V形图标出现,然后消失,然后再次出现,等等。只要用户在页面上。

最佳答案

试试这个

$('#chevron').animate({'opacity': 1}, {
  duration: 500, 
  complete: animateChevron
});

你也可以做得更好

function animateChevron() {       
 $('#chevron').animate({'opacity': 1}, {
   duration: 500         
 }).animate({'opacity': 0.1}, {
  duration: 500,
  complete: animateChevron    
 });        
}

关于javascript - 为什么我的 jquery animate() 回调会导致溢出?递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608632/

相关文章:

javascript - Chrome 应用程序文件系统。无法写入本地文件

jquery - CSS - 将绝对 div 定位为垂直和水平 float

javascript - 如何在 JavaScript 中使用来自 db/php 的 JSON

python - 递归调度算法不能正常工作 Python

python - 如何在Python中编写一个递归生成向量的函数?

javascript - HTML 菜单上的 addClass/removeClass 不起作用

javascript - 如何确定我的哪些标记在我的 Google map 中可见?

javascript - 以编程方式构造在 if() 语句中使用的条件

javascript - 来自 jQuery 类的多个 div 的鼠标悬停()事件

python - py2exe打包py文件时出现"maximum recursion depth exceeded"