2017-03-13 2 views
0

javascriptの助けを借りて各変更の間にカスタム遅延を設定して、無限ループ内のページの背景を変更したいと思います。 私はいくつかの問題に遭遇した:私はループを追加するときカスタム遅延を伴うjavascriptを使用してループ内で動的にラッパーの背景を変更する

setTimeout(myFunction_1, 3000); 
setTimeout(myFunction_2, 3000); 

function myFunction_1(){     
document.getElementById("wrapper").style.background = "green"; 
console.log("test 1"); 
} 

function myFunction_2(){     
document.getElementById("wrapper").style.background = "yellow"; 
console.log("test 2"); 
} 

アン:

私は両方の機能を同時に立ち上げて、何らかの理由で、最初のループせずにそれをテスト

while (1 == 1){ 
setTimeout(myFunction_1, 3000); 
setTimeout(myFunction_2, 3000); 
} 

それはブラウザをクラッシュします...

ブラウザをクラッシュせずにカスタム時間間隔でループの背景を変更するにはどうすればよいですか?

答えて

1

あなたはjsのものに慣れていないようですが、最初はもっとうまくいきます。

あなたは以下のようにコードを変更する必要があります。

myFunction_1(); 

function myFunction_1(){     
    document.getElementById("wrapper").style.background = "green"; 
    console.log("test 1"); 
    setTimeout(function(){ 
     myFunction_2(); 
    },3000); 
} 

function myFunction_2(){     
    document.getElementById("wrapper").style.background = "yellow"; 
    console.log("test 2"); 
    setTimeout(function(){ 
     myFunction_3(); 
    },3000); 
} 

function myFunction_3(){     
    document.getElementById("wrapper").style.background = "blue"; 
    console.log("test 3"); 
    setTimeout(function(){ 
     myFunction_1(); 
    },3000); 
} 
+0

私はNの背景を持っている場合、私は同様の方法でそれらすべてをチェーンすることができますか? – Acidon

+0

3つの機能に合わせてコードを変更しました。あなたは、あなたの色と時間のギャップをどのように整理するかを示すために、データ配列または何かを与えることをお勧めします。私はコードのアーキテクチャを決定することができます。 – blackmiaool

+0

私のデータはPHP配列であり、javascriptはPHPで動的に作成されます。私はあなたがすでに私に与えたもので働くことができると思う。あなたは正しかった、私はjsの新しいとちょうど学ぶようになった:) – Acidon

関連する問題