0

My issue at the moment is that I have a function that takes ~600ms to complete. If a user spam clicks the selector it invokes multiple calls. I have tried to stop spam clicking by disabling the click event callback when the function is called, however, this does not work. Is there any way to stop mouseclicks or remove queued callbacks.

$('.selector').click(function() {
    $(this).off('click.disabled');
    //DO STUFF
    $(this).on('click.disabled', false);
});

Thanks!

4

2 回答 2

0
var clickTime = -1;

$('.selector').click(function() {
    if (clickTime == -1) {
        clickTime = new Date().getTime();
    }
    var end = new Date().getTime();
    var time = end - clickTime;
    if (!$(this).hasClass('selected') && time < 500){
        e.addClass("selected");
        // Do other stuff
        clickTime = new Date().getTime();
       }
});

我现在要使用这个。很可能需要对事件调用进行更多研究,并想出一种不同的方法来防止这种情况发生,因为我不喜欢我的 hacky 解决方案。

无论如何,任何反馈都将不胜感激。

于 2014-10-30T13:23:41.510 回答
0

可以使用布尔值来获取状态:

var clicked = false;
$('.selector').click(function() {
    if (!clicked){
        clicked = true;
        ...
        clicked = false;
    }
});

或使用“一个”方法仅附加一次处理程序

var myFunc = function(){
    ...


   //on finish 
   $('.selector').one( "click", myFunc);
};

$('.selector').one( "click", myFunc); 
于 2014-10-30T12:40:25.410 回答