2016-11-21 43 views
3

我試圖編寫一個程序來練習我的js技能。有3個球,他們首先被隱藏起來。我想先ball_1出現,1秒後,ball_1消失。接下來,ball_2出現,1秒後消失;同樣的邏輯與ball_3一致。當我運行我的代碼時,前兩個球不會隱藏。我不確定發生了什麼問題。下面的代碼是我寫的html,css和js代碼。希望有人能幫助我。先謝謝你。嘗試setTimeout來顯示和隱藏球

$(document).ready(function() { 
 
    var notes = ['ball_1', 'ball_2', 'ball_3']; 
 
    for (i = notes.length; i > 0; i--) { 
 
    var note = notes.shift(); 
 
    $('#' + note).addClass('shown'); 
 
    setTimeout(function() { 
 
     $('#' + note).removeClass('shown'); 
 
    }, 1000); 
 
    } 
 
});
#ball_1 { 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #000000; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 
#ball_2 { 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #0000FF; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 
#ball_3 { 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #7FFF00; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 
#ball_1, 
 
#ball_2, 
 
#ball_3 { 
 
    width: 10px; 
 
    height: 10px; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 
.not_shown { 
 
    display: none; 
 
} 
 
.shown { 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
 

 
<div id="ball"> 
 
    <div id="ball_1" class="not_shown"></div> 
 
    <div id="ball_2" class="not_shown"></div> 
 
    <div id="ball_3" class="not_shown"></div> 
 
</div>

回答

1

您正在尋找事件的asnychronous發揮 - 第一ball_1顯示了1秒和ball_2顯示了1秒等等。

像這樣的東西是行不通的:

for(var i = 0; i < notes.length; i++){ 
     $('#' + notes[i]).addClass('shown'); 
     setTimeout(function() { 
      $('#' + notes[i]).removeClass('shown'); 
     },1000); 
    } 

因爲timeout旨意註冊一個接一個快速連續地後,所有的球會在小顯示和隱藏一秒多

所以,你可以創建一個callback並設置之前的球已經充分顯示了1秒後,才進行下一個球超時 - 看演示如下:

$(document).ready(function() { 
 
    var notes = ['ball_1', 'ball_2', 'ball_3']; 
 
    hideBall(notes,0); 
 
}); 
 

 
function hideBall(notes,i) { 
 
    $('#' + notes[i]).addClass('shown'); 
 
    hide(function() { 
 
    if(++i < notes.length) { 
 
     hideBall(notes,i); 
 
    } 
 
    }, notes[i]); 
 
} 
 

 
function hide(callback, note) { 
 
    setTimeout(function() { 
 
     $('#' + note).removeClass('shown'); 
 
     callback(); 
 
    }, 1000); 
 
}
#ball_1 { 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #000000; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 
#ball_2 { 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #0000FF; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 
#ball_3 { 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #7FFF00; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 
#ball_1, 
 
#ball_2, 
 
#ball_3 { 
 
    width: 10px; 
 
    height: 10px; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 
.not_shown { 
 
    display: none; 
 
} 
 
.shown { 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
 
<div id="ball"> 
 
    <div id="ball_1" class="not_shown"></div> 
 
    <div id="ball_2" class="not_shown"></div> 
 
    <div id="ball_3" class="not_shown"></div> 
 
</div>

+0

非常感謝您幫助我理解「回調」的概念。我從來沒有學過它。我有一個問題,在「隱藏」函數中,有一個函數「callback()」。這是一個JavaScript中的內置方法,因爲我沒有看到你寫函數。第二個問題是顯示了三個球在同一個地方,有沒有什麼辦法讓ball_1顯示在最上面的位置,ball_2顯示在中間,而ball_3在底部,就像在html中重寫的順序一樣?再次非常感謝你!! – vkosyj

+0

'callback()'與函數的參數'callback'相同,對於其他疑問,您可以使用'visibility:visible'和'visibility:hidden',而不是切換'display' ....讓我知道如果您遇到問題實施:) – kukkuz

+0

謝謝你的回覆,我發現這對我來說是充滿了理解的一種挑戰,所以基本上callback()實際上是hideball(),所以我可以想到hideball()在一段時間後自動調用自己的代碼 – vkosyj

2

通常,當迭代使用for循環從未修改的陣列。 shift方法將從數組中移除第一項,從而修改它的長度。相反,這樣做:

$(document).ready(function() { 
    var notes = ['ball_1','ball_2','ball_3']; 
    var i; // You were declaring "i" in global namespace before. Don't do that. 
    for(i = 0; i < notes.length; i++){ 
    var note = notes[i]; 
    $('#' + note).addClass('shown'); 
     setTimeout(function() { 
     $('#' + note).removeClass('shown'); 
     },1000); 
    } 
}); 

另外,你會從我的筆記中看到,你正在全局命名空間中定義「我」。如果使用「var」,那麼始終確保在功能塊的開頭定義變量永遠不會那麼好。

編輯:錯過了一個分號

EDIT2:徹底無緣,我需要改變了循環條件。

1

希望這是你所需要的

$(document).ready(function() { 
 
    var notes = ['ball_1','ball_2','ball_3']; 
 
    for(i = notes.length; i > 0; i--){ 
 
     var note = notes[i]; 
 
     $('#' + note).addClass('shown'); 
 
     hideBall(note, i) 
 
    } 
 
}); 
 

 
function hideBall(note) { 
 
    setTimeout(function() { 
 
     $('#' + note).removeClass('shown'); 
 
    },1000 * i); 
 
}
#ball_1{ 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #000000; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 

 
#ball_2{ 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #0000FF; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 

 

 
#ball_3{ 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #7FFF00; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 

 
#ball_1, #ball_2, #ball_3 { 
 
    width: 10px; 
 
    height: 10px; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 

 
.not_shown { 
 
    display: none; 
 
} 
 

 
.shown { 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id = "ball"> 
 
     <div id = "ball_1" class = "not_shown"></div> 
 
     <div id = "ball_2" class = "not_shown"></div> 
 
     <div id = "ball_3" class = "not_shown"></div> 
 
    </div>

+0

感謝ü。但我想要的是顯示ball_1並在1秒後消失;然後ball_2出現並在1秒後消失等 – vkosyj

1

你正在嘗試將無法工作,因爲它會運行的循環中的所有一氣呵成,設立3倍超時。

嘗試這樣的事情

jQuery(document).ready(function($) { 
function myBallLoop(){ 
    // increment as needed 
    if(typeof note == 'undefined') { 
     var note = 1; 
    } else if (note == 3){ 
     break; // end loop 
    } else { 
     note ++; 
    } 
     // show current ball qickly 
     $('#ball_' + note).show('fast', function(){ 
      // call back after show event 
      // hide current ball after 1 sec 
      r = setTimeout(function(){$('#ball_' + note).hide()}, 1000); 
      // self call function after 2 seconts 
      t = setTimeout(function(){myBallLoop();, 2000} 
     });   

    } 
    // loop start 
    myBallLoop(); 
}); 
1

充分利用jquery給你的。

使用$ .each進行迭代也與ES5的forEach相同。使用延遲方法來延遲添加類的功能類似於setTimeout。

$(document).ready(() => { 
 
    var notes = ['ball_1','ball_2','ball_3']; 
 

 
    let showBalls = (i, item) => { 
 
    $('#' + item).delay(i * 1000).queue(() => { 
 
     $('#' + item).addClass('shown'); 
 
     $('#' + notes[i - 1]).removeClass('shown').clearQueue(); 
 
    }); 
 
    } 
 
    $.each(notes, (i, item) => { 
 
    showBalls(i, item); 
 
    }); 
 
});
#ball_1{ 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #000000; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 

 
#ball_2{ 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #0000FF; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 

 

 
#ball_3{ 
 
    width: 10px; 
 
    height: 10px; 
 
    background: #7FFF00; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 

 
#ball_1, #ball_2, #ball_3 { 
 
    width: 10px; 
 
    height: 10px; 
 
    border: 2px solid #ccc; 
 
    border-radius: 50%; 
 
} 
 

 
.not_shown { 
 
    display: none; 
 
} 
 

 
.shown { 
 
    display: block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id = "ball"> 
 
    <div id = "ball_1" class = "not_shown"></div> 
 
    <div id = "ball_2" class = "not_shown"></div> 
 
    <div id = "ball_3" class = "not_shown"></div> 
 
</div>

+0

謝謝赫爾曼幫助我。我很抱歉沒有說清楚。我想讓黑球出現,1秒後黑球消失。接下來,藍色球出現,1秒後消失;綠球也是一樣的邏輯。 – vkosyj

+0

@vkosyj爲你更新了我的答案。希望它也有幫助。 :) –

+0

再次謝謝你赫爾曼。然而,綠球仍然存在。無論如何也要讓它消失嗎?非常感謝。我剛剛瞭解了一些關於js的內容,關於jquery的不多。我必須學習像「=>」這樣的花式符號。 – vkosyj