2012-04-09 36 views
0

我結合一些稍微複雜的功能,點擊事件jQuery的 - 開心唱首歌 - 尋找回調的綁定事件

$(someSelector)).bind('click', someFunction(a,b,c)); 

function somefunction(a,b,c) { 
    return function() { 
     // some logic 
     $(anotherSelector).each(function() { 
      // fade out with call back... 
      $(this).fadeOut("fast", function() { 
       // TODO: add callback here??? 
       $(contentSelector).fadeIn("fast"); 
      }) 
     }) 

    } 
} 

的問題是,一系列的矛盾淡出/淡入行爲快速點擊的結果。我假設在當前fadeIn完成之前處理新的點擊事件。

我想我正在尋找一些回調機制,確保在點擊完成後處理新的點擊。我可以添加一個回調到fadeIn,但我不明白我的邏輯類型會對我有什麼幫助...

我還閱讀了有關解除綁定組件的處理(稍後重新綁定它們) ,但是再一次 - 我不確定我會把它放在哪個邏輯之上。

感謝您的建議:)

+1

如果我的理解正確,.stop()函數可能對您有些用處:http://api.jquery.com/stop/ – 2012-04-09 07:26:06

+0

Thx,但stop()將簡單地取消正在進行的動畫 - 這不是我在這裏找的 – 2012-04-09 09:04:04

回答

1

你可以使用jQuery的.on.off方法:

$someElement = $(someSelector) 
$someElement.on('click', someFunction(a, b, c)); 

function someFunction(a, b, c) { 
    // detach the event handler 
    $someElement.off('click', someFunction); 

    /* useful stuff here */ 

    // reattach the event handler 
    $someElement.on('click', someFunction(a, b, c)); 
} 

在那裏我有cached the jQuery element

或者,你可以創建回調的範圍的變量外,檢查和設置它的回調內在價值:

$(someSelector)).bind('click', someFunction(a,b,c)); 

var isWaiting = false; 
function somefunction(a,b,c) { 
    if (!isWaiting) { 
    isWaiting = true; 

    /* useful stuff here */ 

    isWaiting = false; 
    } 
} 

我會建議.on/.off方法,因爲它與jQuery的事件涉及更直接綁定過程。