我可以用jQuery检测到按钮上的点击
$('#myButton').click(function(){
// do something
});但是,当用户多次单击按钮时,它会触发不必要的中介事件。
我只想在最后一次点击按钮时触发事件。
类似于:
$('#myButton').lastClickOnASequenceOfClicks(function(){
// ignore the multiple clicks followed
// do something only on the last click of a sequence of clicks
});这样,如果用户单击10次(有一段时间间隔),它应该只在第十次单击时触发一个事件。
发布于 2014-08-01 22:47:03
每次单击都会重置计时器。
var timer;
$("#myButton").click(function () {
var timeToWait = 1000;
clearTimeout(timer);
timer = setTimeout(function () {
// do something only on the last click
}, timeToWait);
}更新
另一种解决由用户生成的“多点击事件”问题的方法是执行OP注释部分中提到的操作。do something在第一次单击,然后禁用该按钮,因此用户不能再单击它(也可能设置一个时间,使该按钮再次启用)
var timer, timeToWait = 5000, selector ="#myButton";
$(selector).click(function (e) {
$(this).attr("disabled", "disabled");
// do something
// Then wait a certain amount of time then remove the disabled attr on your button
timer = setTimeout(function () {
$(selector).removeAttr("disabled");
}, timeToWait);
})https://stackoverflow.com/questions/25089727
复制相似问题