您可以設置一個標誌,檢查是否確定運行foo()
功能,或者取消綁定,你不希望用戶能夠使用它,然後在活動時間內重新綁定的事件處理程序延遲後(只是幾個選項)。
這是我會做的。我會用一個超時排除後續事件:
$(document).delegate('#my-page-id', 'pageinit', function() {
//setup a flag to determine if it's OK to run the event handler
var okFlag = true;
//bind event handler to the element in question for the `click` event
$('#my-button-id').bind('click', function() {
//check to see if the flag is set to `true`, do nothing if it's not
if (okFlag) {
//set the flag to `false` so the event handler will be disabled until the timeout resolves
okFlag = false;
//set a timeout to set the flag back to `true` which enables the event handler once again
//you can change the delay for the timeout to whatever you may need, note that units are in milliseconds
setTimeout(function() {
okFlag = true;
}, 300);
//and now, finally, run your original event handler
foo();
}
});
});
我不知道使用跟蹤變量提到了原來的問題,但3小時google搜索,試錯後,這個作品一種享受!從我+1。 – 2012-08-10 10:24:11
是的,如果你不是一個單獨的變量,你可以將標誌變量存儲爲jQuery數據。 '.data(this,'flag',[state])' – Jasper 2012-08-10 16:11:21