简体   繁体   English

jQuery防止在用户双击或多次点击的情况下执行点击事件

[英]jQuery preventing click event to be executed in case of double or multiple clicks by user

I have some code in which I want to stop user from clicking a button multiple times. 我有一些代码,我希望阻止用户多次单击按钮。 I have tried multiple things including disabling button on click and enabling it at the end but nothing is working perfectly. 我已经尝试了多种功能,包括在点击时禁用按钮并在最后启用它,但没有任何工作完美。

I want to stop "click" event of jQuery (single click) from being executed in case user has clicked on it two or more times. 我希望停止执行jQuery(单击)的“click”事件,以防用户点击它两次或更多次。

This is my js fiddle: https://jsfiddle.net/tm5xvtc1/6/ 这是我的小提琴: https//jsfiddle.net/tm5xvtc1/6/

<p id="clickable">Click on this paragraph.</p>
<p id="main">
I will change on clicking
</p>

$("#clickable").click(function(){
    $('#main').text('Single click');
});

$("#clickable").dblclick(function(){
    $('#main').text('Double click')
});

If i try double clicking, the behavior is: Single click gets executed first => Then double click gets executed. 如果我尝试双击,行为是:首先单击执行=>然后双击执行。

I want to prevent single click event to be executed in case user clicks on button multiple times. 我希望防止单击事件被执行,以防用户多次点击按钮。 Suggestions? 建议?

This is bit tricky. 这有点棘手。 You need to calculate the time taken for double click and trigger events. 您需要计算双击和触发事件所需的时间。 Try the below code 请尝试以下代码

$(function() {

  var clicks = 0;
  var timer = null;

  $("#clickable").on("click", function(e) {
      clicks++; // Increment click counter
      if (clicks === 1) {
        timer = setTimeout(function() {
          $('#main').text('Single click');
          clicks = 0; //Reset
        }, 800); // Increase or decrease if there is slowness or speed
      } else {
        clearTimeout(timer); //prevent single-click action
        $('#main').text('Double click')
        clicks = 0; // Reset
      }
    });

    $("#clickable").on("dblclick", function(e) {
      e.preventDefault(); //Prevent double click
    });

});

Demo : https://jsfiddle.net/cthangaraja/e9e50jht/2/ 演示: https//jsfiddle.net/cthangaraja/e9e50jht/2/

According to the jquery documentation: 根据jquery文档:

It is inadvisable to bind handlers to both the click and dblclick events for the same element. 将处理程序绑定到同一元素的clickdblclick事件是不可取的。 The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one. 触发的事件序列因浏览器而异,有些事件在dblclick之前接收到两个click事件,而其他事件只接收一个事件。 Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable. 双击敏感度(双击检测到的点击之间的最长时间)可能因操作系统和浏览器而异,通常可由用户配置。

That being said, you can accomplish what you want by using $('#main').addClass('clicked-once'); 话虽这么说,你可以通过使用$('#main').addClass('clicked-once');来实现你想要的$('#main').addClass('clicked-once'); and testing for the existence of that class before executing the code inside the single click handler. 并在单击处理程序中执行代码之前测试该类是否存在。

$("#clickable").click(function(){
    if($(this).hasClass('clicked-once')){
        return false;
    } else {
        $(this).addClass('clicked-once');
        $('#main').text('Single click');
   }
});

$("#clickable").dblclick(function(){
    $('#main').text('Double click')
});

jsfiddle: https://jsfiddle.net/nbj1s74L/ jsfiddle: https ://jsfiddle.net/nbj1s74L/

I found the answer for this. 我找到了答案。

$(document).on('click', '#clickable', function () {
    $(this).prop('disabled', true);
    //LOGIC
    setTimeout(function () { $(this).prop('disabled', false); }, 500);
});

Its working for me. 它为我工作。 The set timeout for 500ms doesn't allow code to be re-entrant which is working fine for me at various network/device speeds. 500ms的设置超时不允许代码重入,这对于我在各种网络/设备速度下工作正常。

Slight change in the answer given by maverick . 特立独行给出的答案略有变化。

In the set timeout method, reference of this is changed. 在set timeout方法中,更改了this引用。 Hence the code should be changed to: 因此,代码应更改为:

$(document).on('click', '#clickable', function () {
    var self = this;
    $(this).prop('disabled', true);
    //LOGIC
    setTimeout(function () { $(self).prop('disabled', false); }, 500);
});
window.numOfClicks = 0

$("#clickable").click(function(){
    window.numOfClicks += 1;
    //rest of code
});

Record the number of clicks to use for your functions, example: 记录您的功能使用的点击次数,例如:

if (window.numOfClicks > 1){ //do this}

If you need it reset just put a timeout in the .click() 如果你需要它重置只是在.click()中加载一个超时

var resetClicks = function(){ window.numOfClicks = 0 }

$("#clickable").click(function(){
    //your other code
    setTimeout(resetClicks,5000); //reset every 5 seconds
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM