Last active
January 11, 2018 03:48
-
-
Save MJ111/bd17ed25ed850f2c5d57b4f6e0ab9d54 to your computer and use it in GitHub Desktop.
[Disable Button during Ajax] #javascript
This file contains 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
/** | |
* 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