Created
April 5, 2018 09:37
-
-
Save simonfranzen/c4f04f555e82af444eb4b1dc9fcb90fa to your computer and use it in GitHub Desktop.
Small jQuery helper to disable buttons while AJAX request is loading.
This file contains hidden or 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
// 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