Skip to content

Instantly share code, notes, and snippets.

@simonfranzen
Created April 5, 2018 09:37
Show Gist options
  • Save simonfranzen/c4f04f555e82af444eb4b1dc9fcb90fa to your computer and use it in GitHub Desktop.
Save simonfranzen/c4f04f555e82af444eb4b1dc9fcb90fa to your computer and use it in GitHub Desktop.
Small jQuery helper to disable buttons while AJAX request is loading.
// Small jQuery helper to disable buttons while AJAX request is loading.
// Init the helper for all buttons on page with $('.js-await-response').awaitResponse()
// If a button with your class was klicked, the button itself and all others with that class will be disabled until the response came back
(function ( $ ) {
$.fn.awaitResponse = function( options ) {
var self = this;
// listening to starting AJAX events
$( document ).ajaxSend(function( event, jqxhr, settings ) {
self.attr('disabled', '');
});
// listening to completed AJAX events
$( document ).ajaxComplete(function(e) {
self.attr('disabled', null);
});
};
}( jQuery ));
$(document).ready(function(e){
$('.js-await-response').awaitResponse();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment