2013-03-05 56 views
4
$("#prevPage").live("click",function(e) { 
................. 
}); 

例如,當用戶已經點擊prevPage時,裏面的語句正在運行,如果用戶立即點擊它,它會再次觸發。不過,我希望點擊事件觸發器只有在它內部的所有語句都完成執行後,如何實現這一點?謝謝。如何在執行時臨時禁用點擊功能?

+1

使用.on()而不是.live(),因爲.live()不推薦使用。 – Swarne27 2013-03-05 04:52:20

+0

感謝提醒 – user782104 2013-03-05 04:54:13

回答

2

這個怎麼樣或者類似的東西:

<script type="text/javascript"> 
    // disable command while function is being executed. 
    var sample = { 
     isExecuting : 0, 
     doWork : function (e) { 
      if (sample.isExecuting === 1) return; 
      sample.isExecuting = 1; 
      // do work -- whatever you please 
      sample.isExecuting = 0; // say: I'm done! 
     } 
    }; 
    // live or bind 
    $("#prevPage").bind("click",function(e) { 
     sample.doWork(e); 
    }); 
</script> 

簡單的「盾牌」來阻止多呼叫方案。

+0

如果像'settimeout'這樣的異步執行,在「做工 - 無論你喜歡什麼」時該怎麼辦。 – rab 2013-03-05 05:08:18

+0

然後您推遲「unflag」(sample.isExecuting = 0),直到操作完成。你在異步操作中有回調函數嗎? – 2013-03-05 05:26:29

+0

async ajax或worker?我將提供一個代碼示例。在所有異步場景中,有一些時刻需要同步執行某些操作。知道適用的應用程序是應用程序開發的先決條件。等待反饋 – 2013-03-05 05:28:32

0

使用jquery

$("#prevPage").unbind("click"); 

的解除綁定功能後,你的任務完成了

$("#prevPage").bind("click",function(){....your code here....}); 
+0

謝謝。如果所有的聲明完成後怎麼辦? – user782104 2013-03-05 04:50:52

+0

你應該使用[die](http://api.jquery.com/die/),但在jQuery 1.9中知道'live'和'die'已被**刪除**。 – elclanrs 2013-03-05 04:52:15

+0

$(「#prevPage」)。bind(「click」,function(){}); – 2013-03-05 04:55:00

1

然後設置元件上的標誌,以檢查它是否可點擊與否。

$("#prevPage").on("click",function(e) { 

    e.preventDefault(); 

    //get the clickable attribute 
    //if it's not existent, its undefined hence "false" 
    var unclickable = this.unclickable; 

    //if it's not unclickable (it's clickable) 
    if(!unclickable){ 

    //make the flag unclickable 
    this.unclickable = true; 

    //do stuff 

    //reset it back the way it was after operations 
    this.unclickable = false; 
    } 


}); 
0

設置您的事件觸發

var prevPageEventTriggered = false ; 

並將其設置爲ture當事件觸發

prevPageEventTriggered = true; 

,然後單擊事件處理函數添加條件這個變量

$("#prevPage").live("click",function(e) { 

     if (prevPageEventTriggered) { 
       return false; 
     } 

     // your code goes here 
     // .... 
}); 

如果它已完成執行,則可以將其設置爲false。希望這將有助於

0
  • 解除綁定()將做的工作適合你。

  • 替代方案可能像使用detach()。當您的進程正在執行分離按鈕並且當您的進程執行完成時,請使用重新附加()來獲取按鈕。 我會建議的是使用unbind()

相關問題