1

私はこのスニペットを取りましたが、それは美しく機能しますが、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(); 
    }       
});
4

2 に答える 2

3

アニメーションが完了したときではなく、できるだけ早くその関数を実行します。これにより、関数はおそらく毎秒数百回実行され、再帰の問題が発生します。

$(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);
    }
});
于 2012-07-25T20:53:29.863 に答える
0

これは、アニメーションが完了した後に呼び出しが発生するのではなく、再帰呼び出しが常に発生しているためです。代わりに、次のようなアニメーション関数のコールバックに呼び出しを入れます。

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();
    });
}​
于 2012-07-25T20:53:56.713 に答える