Skip to content

Instantly share code, notes, and snippets.

@yairEO
Last active September 5, 2017 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yairEO/83bd087aa7a74212c607c34b47fe5382 to your computer and use it in GitHub Desktop.
Save yairEO/83bd087aa7a74212c607c34b47fe5382 to your computer and use it in GitHub Desktop.
jQuery special-event "clickLimit" [only fires the event if a duration threshold hasn't passed]
jQuery.event.special.clickLimit = {
delegateType: "mousedown",
bindType: "mousedown",
handle: function( event ) {
var handleObj = event.handleObj,
startTime = new Date().getTime(),
threshold = event.data.threshold || 500;
$(this)
.unbind('mouseup.clickLimit')
.bind('mouseup.clickLimit', function(){
var delta = new Date().getTime() - startTime;
delta < threshold && fire();
})
function fire(){
event.type = handleObj.origType;
handleObj.handler.apply( this, arguments );
event.type = handleObj.type;
}
return null;
}
};
@yairEO
Copy link
Author

yairEO commented Sep 5, 2017

Example:

$("body").on( "clickLimit", {threshold:100}, function(e){
    var randomColor = "#"+((1<<24)*Math.random()|0).toString(16);
    document.body.style.background = randomColor;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment