首先感謝您閱讀。
我有一個問題停止執行功能,我會盡力解釋我想要做什麼以及我如何去做。
例如,我在元素a or button
上綁定了一些事件,並且我希望用戶不會再次單擊它,我的意思是他可以單擊它,但所有操作都不會被調用一段時間(比如說一秒)。停止執行JS中的委託函數
所以,這裏是一些例子:
在HTML1st example
<ul>
<li><a href="some url" class='button'>Click me</a></li>
<li><a href="some url" class='button'>Click me</a></li>
<li><a href="some url" class='button'>Click me</a></li>
</ul>
2nd example somewhere else:
<section id='someid'>
some info
some articles - doesn't matter
<button class='button iLoadComments'>Load more comments</button>
</section>
在javascript:
$("ul").on("click","a.button",function(){
... here some code or ajax request, whatever
return false;
});
$("section").on("click","iLoadComments",function(){
... some ajax request
return false;
})
,我想,不允許執行它。 ,我想通過1個功能涵蓋所有代碼..
所以,我做了什麼:我在全球範圍定義的新事件偵聽器,並添加屬性disabled
該元素
$("body").on("mousedown", ".button", function(e){
var $this=$(this);
if ($this.attr("disabled")) {
var event = e || window.event;
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
event.preventDefault();
setTimeout(function() {
$this.removeAttr("disabled");
}, 1000);
return false;
}else{
$this.attr("disabled", true);
}
});
但問題delegeted功能執行任何方式((
能有人幫助請,豈不是更好的做法?或者方法來糾正呢?
'了'元素沒有一個'disabled'屬性。 – cdmckay