これは、あなたが思っているよりも一般的な問題です。問題は、要素を に設定するイベントが設定されていることですdisplay: none
。しかし、もう一度クリックすると、そのイベントが発生しないようにする必要があります。したがって、要素の非表示をキャンセルする必要がある場合にその参照setTimeout()
を呼び出すことができるように、イベントへの参照を保持する必要があります (これを返します)。clearInterval()
var listener = (function() {
var interval = 0; //A variable to retain the interval
return function() { //return the original function into listener
var title = document.getElementById('title');
//dismiss any plans to hide/show the element because we've changed our minds
// (if the event already fired, this does nothing, which is fine):
clearInterval(interval);
if (title.dataset.displayed == 'yes') {
title.dataset.displayed = 'no';
setTimeout(function () {
title.style.webkitTransition = "opacity 2.0s ease";
title.style.opacity = 0.0;
}, 0);
//don't forget to hold onto the interval!
interval = setTimeout(function() { title.style.display = 'none'; }, 2000);
}
else {
title.dataset.displayed = 'yes';
title.style.display = 'block';
//It's probably not necessary to retain the interval here.
// But it doesn't hurt anything.
interval = setTimeout(function () {
title.style.webkitTransition = "opacity 2.0s ease";
title.style.opacity = 1.0;
}, 0);
}
};
}());
var li = document.getElementById('list');
li.addEventListener("click", listener, false);
元の関数を返す、すぐに呼び出されるより大きな関数式内にリスナー関数をラップしたことに注意してください。interval
ある呼び出しから次の呼び出しまで保持されるように、これを行いlistener()
ました。グローバル変数を作成することもできinterval
ますが、それらはいたずらであり、このソリューションははるかに楽しく自己完結型です。
JSFiddle にスワップするとうまくいくようです。残りのコードもお疲れ様でした!