Skip to content

Instantly share code, notes, and snippets.

@ggilder
Created May 11, 2011 21:50
Show Gist options
  • Save ggilder/967456 to your computer and use it in GitHub Desktop.
Save ggilder/967456 to your computer and use it in GitHub Desktop.
jQuery blocking events - prevent an event handler from re-triggering until you unblock it
(function($){
$.fn.blockingEvent = function(eventType, eventHandler){
var eventId = '_active_'+eventType;
this.bind(eventType, function(e){
e.preventDefault();
$this = $(this);
if ($this.data(eventId)){
return false;
}
$this.data(eventId, true);
eventHandler.apply(this, arguments);
})
return this;
};
$.fn.unblockEvent = function(eventType){
var eventId = '_active_'+eventType;
this.data(eventId, false);
return this;
};
})(jQuery);
@ggilder
Copy link
Author

ggilder commented May 11, 2011

$('a').blockingEvent('click', function(e){
    $(this).empty().append('<div class="loading"></div>');
    // do some stuff
    $(this).unblockEvent('click');
});

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