2009-07-02 80 views
2

我正在使用像下面這樣的Jquery時間延遲推子,但需要根據代碼中的事件重新初始化它,即將計時器敲回零。我該怎麼做呢?我試過再次調用fader()。jQuery如何重新初始化延遲函數

function fader(){ 

$(ajdiv).delay(10000, function(){$(ajdiv).fadeOut()}); 

} 
jQuery.fn.delay = function(time,func){ //run a delayed function 

return this.each(function(){ 

    setTimeout(func,time); 

}); 

}; 

回答

1

您需要保存從setTimeout返回的timeoutID。您可以調用window.clearTimeout(timeoutID)來移除計時器並停止事件的發生。在「重置」的情況下,您必須事後創建一個新的。

該mozilla documentation for setTimeout

退房this question也有一個(在我看來)更優雅的解決方案,我認爲你有同樣的問題。

+0

謝謝! :D你知道這是否也適用於其他瀏覽器? – user132192 2009-07-02 13:01:31

1

這是我的,我用實際的代碼(它使來自左支撐杆的新元素玻片給我的自定義延時功能的時間,然後再次消失):

$element.next().next().hide().show('slide', {direction : 'left'}, 700, function() {$(this).delay(10000, function() {$element.next().next().hide('slide', {direction: 'left'}, 700)})});