Skip to content

Instantly share code, notes, and snippets.

@MJ111
Last active January 11, 2018 03:48
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 MJ111/bd17ed25ed850f2c5d57b4f6e0ab9d54 to your computer and use it in GitHub Desktop.
Save MJ111/bd17ed25ed850f2c5d57b4f6e0ab9d54 to your computer and use it in GitHub Desktop.
[Disable Button during Ajax] #javascript
/**
* disable clicked button before jQuery ajax request and enable after ajax request finished
* @param {Function} ajaxFn
* ajaxFn should return executed jQuery AJAX function
* @returns {Function}
* returned function should be placed in event listener of Element
*
* Usage:
* $('#deactivate-btn').click(disableBtnDuringAjax(function(e) {
* return $.get('/api/users', function (data) {
* console.log(data);
* })
* .fail(function() {
* console.log(data);
* })
* }));
*/
export function disableBtnDuringAjax(ajaxFn) {
return function(e) {
const btn = $(e.target)
btn.attr('disabled', true);
ajaxFn().always(function () { // jQuery specific code(always)
btn.attr('disabled', false);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment