我試圖實現我的夢想,在繪製無用的廢話。但是在10分鐘內學習jQuery之後,我遇到了延遲函數無法正常工作的問題。jQuery setTimeout /延遲循環+鼠標中心
所以,我有500個黑色小divs與類「luls」。這個想法是,當你用鼠標懸停在它們上面時,它們以隨機顏色點亮,並在一段時間後取回黑色。但他們不是。
以Picture。
$(document).ready(function() {
$(".luls").each(function(index) {
$(this).mouseenter(function() {
// It's just a random color function, do not focus on it.
var hue = 'rgb(' + (Math.floor((256-199)*Math.random()) + 200) + ',' + (Math.floor((256-199)*Math.random()) + 200) + ',' + (Math.floor((256-199)*Math.random()) + 200) + ')';
$(this).css("background-color", hue);
});
setTimeout(function() {
$(this).css('background-color', 'black');
}, 1000);
});
});
我試過使用delay()和setTimeout,甚至是mouseover,但它不起作用。需要你的幫助。
我認爲setTimeout的重新綁定 '本' 的窗口。所以在setTimeout中調用$(this)可能會給你帶來意想不到的結果。 –
另外,您正在'mouseenter'回調之外調用'setTimeout' - 即。在最初的負載。 –
嘗試將set timeout設置在每個回調中 – Volem