-
-
Save kazimolmez/d65c43861ede6492128bf7d72fd86382 to your computer and use it in GitHub Desktop.
Prevent double click!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery plugin to prevent double click | |
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; | |
}; | |
// OR | |
$("button").on('click', function (event) { | |
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) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment