How to prevent a double-click using jQuery?

前端 未结 15 825
孤街浪徒
孤街浪徒 2020-11-30 23:42

I have a button as such:


Within jQuery I am using the following, but it still

15条回答
  •  执念已碎
    2020-12-01 00:26

    My solution: https://gist.github.com/pangui/86b5e0610b53ddf28f94 It prevents double click but accepts more clicks after 1 second. Hope it helps.

    Here is the code:

    jQuery.fn.preventDoubleClick = function() {
      $(this).on('click', function(e){
        var $el = $(this);
        if($el.data('clicked')){
          // Previously clicked, stop actions
          e.preventDefault();
          e.stopPropagation();
        }else{
          // Mark to ignore next click
          $el.data('clicked', true);
          // Unmark after 1 second
          window.setTimeout(function(){
            $el.removeData('clicked');
          }, 1000)
        }
      });
      return this;
    }; 
    

提交回复
热议问题