禁用点击事件处理程序一段时间 [英] disable click event handler for a duration of time
问题描述
我已经看过类似的问题,但是提供的答案涉及按钮而不是div元素.当我单击ID为click
的div元素时,单击事件处理程序将被unbind()
禁用,并将计时器设置为2秒. 2秒后,应该由bind()
再次启用click事件处理程序.问题是单击事件处理程序似乎没有反弹".我正在将文本附加到另一个div元素,以检查click事件处理程序是否处于活动状态.
I've already looked at similar questions but the answers provided involve buttons and not div elements. When I click the div element with id click
, the click event handler is disabled by unbind()
and sets a timer for 2 seconds. After 2 seconds, the click event handler should be enabled again by bind()
. The problem is that the click event handler doesn't seem to get "rebound". I am appending text to another div element to check if the click event handler is active.
这是我的jsfiddle:
Here is my jsfiddle:
推荐答案
解决整个问题的另一种方法是不必担心取消绑定和重新绑定,而只需使用"disabled"标志即可:
Another approach to the whole problem is not to bother with unbinding and rebinding and just use a "disabled" flag:
$(document).ready(function(){
var clickDisabled = false;
$('#click').click(function(){
if (clickDisabled)
return;
// do your real click processing here
clickDisabled = true;
setTimeout(function(){clickDisabled = false;}, 2000);
});
});
这篇关于禁用点击事件处理程序一段时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!