Created
September 21, 2021 18:25
-
-
Save juliozuppa/64dec1bc0b25bd1e9262ab7cfedc947f to your computer and use it in GitHub Desktop.
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
/** | |
* Evita submeter várias vezes o mesmo formulário ao clicar no botão | |
* submete o form e logo em seguida desativa o botão | |
* que volta a ficar ativo novamente depois do tempo definido em | |
* throttleTime, que por padrão é 30 segundos | |
* | |
* @param button | |
* @param form | |
* @param throttleTime | |
*/ | |
const throttleSubmit = function (button, form, throttleTime = 30000) { | |
button.attr('disabled', true); | |
form.submit(); | |
setTimeout(function () { | |
button.removeAttr('disabled'); | |
}, throttleTime) | |
} | |
// example | |
form.find('#btn-submit').click(function (event) { | |
event.preventDefault(); | |
if (validateForm()) { | |
throttleSubmit($(this), form); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment