2014-07-15 32 views
-4

我需要循環的聯繫總是......沒有停止只是循環請幫我的Javascript環路臨

setTimeout(doSomething, 5000); function doSomething() { 
document.getElementById('myAnchor').innerHTML="Visit Google" 
document.getElementById('myAnchor').href="http://www.google.com" 

setTimeout(doSomething1,5000); } function doSomething1() {                   
document.getElementById('myAnchor').innerHTML="Visit Me" 
document.getElementById('myAnchor').href="http://www.stackoverflow.com" 

setTimeout(doSomething2, 5000); } function doSomething2() { 
document.getElementById('myAnchor').innerHTML="Visit None" 
document.getElementById('myAnchor').href="" } 
+0

也許把'doSomething2'中的超時放在'doSomething'開始的地方? – Bergi

+2

[Fornat your code](http://jsbeautifier.org/)請適當!這是不可讀的!它。 – Bergi

+1

不要自己重複功能。 [讓電腦爲你做。](http://en.wikipedia.org/wiki/Don't_repeat_yourself) –

回答

0
//Data 
var links = [ 
    {text: "Visit Google", href: "http://www/google.com"}, 
    {text: "Visit Me", href: "https://www.stackoverflow.com"} 
], i = 0; 

//Control 
function loop(){ 
    var entry = links[i++%links.length]; 
    document.getElementById('myAnchor').innerHTML = entry.text; 
    document.getElementById('myAnchor').href = entry.href; 
    setTimeout(loop, 5000); 
} 

演示:http://jsfiddle.net/DerekL/TGnMt/

引用我之前的評論:從不重複自己的功能。通過這種思維方式,問題最終會變得更容易解決。

+0

它需要多少時間連續運行以溢出'我'? –

+0

@ PM77-1 - 'Number.MAX_VALUE * 5'秒,這是[約2.848331383307101×10^301平均格里曆年](http://www.wolframalpha.com/share/clip?f=d41d8cd98f00b204e9800998ecf8427edma2jhpcs3)(在Chrome上);) –

+0

你可以請我幫助我...並在盤旋到計時器或循環停止,並取消停止再次啓動 – MultiSubMovies