Skip to content

Instantly share code, notes, and snippets.

@juliozuppa
Created September 21, 2021 18:25
Show Gist options
  • Save juliozuppa/64dec1bc0b25bd1e9262ab7cfedc947f to your computer and use it in GitHub Desktop.
Save juliozuppa/64dec1bc0b25bd1e9262ab7cfedc947f to your computer and use it in GitHub Desktop.
/**
* 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