Jquery Firefox/Firebug recursion



我截取了这个片段,它运行得很好,但如果firebug控制台说"递归太多",firefox/firebug就会消亡这是一篇有类似问题的帖子,我觉得这个问题没有得到正确解决Jquery Too Much Recursion Error

有没有一种方法可以让这种颜色的动画连续循环,而不会产生这种递归问题?如果没有,我如何在没有递归的情况下使其工作?指定到结束的时间量?

$(document).ready(function() {
    spectrum();
    function spectrum(){
        var  hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' +  (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() *  256)) + ')';
        $('#welcome').animate( { backgroundColor: hue }, 1000);
        spectrum(); 
    }       
});

您正在尽可能快地运行该函数,而不是在动画完成时运行。这导致该函数每秒可能运行数百次,从而导致重新诅咒问题:

$(document).ready(function() {
    spectrum();
    function spectrum(){
        var  hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' +  (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() *  256)) + ')';
        $('#welcome').animate( { backgroundColor: hue }, 1000, spectrum);
    }
});

这是因为递归调用不断被触发,而不是在动画完成后被触发。相反,将调用放在animate函数的回调中,如:

spectrum();
function spectrum() {
    var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
    $('#welcome').animate({
        backgroundColor: hue
    }, 1000, function() {
        spectrum();
    });
}​

最新更新