Consider using jQuery.ajax()
where you can make use of beforeSend
callback. This allows you to modify the Button before the HTTP Post. Then you can modify it again in success
callback.
jQuery(function($) {
var int;
var c = 0;
function showLoading(obj) {
int = setInterval(function() {
var t = "Load";
switch (c % 3) {
case 0:
t += "...";
break;
case 1:
t += "..";
break;
case 2:
t += ".";
break;
}
c++;
obj.html(t);
}, 200);
}
$('#btnResendVerificationEmail').click(function() {
$.ajax({
url: "https://reqres.in/api/users",
data: {
token: "token-1234",
action: "resendVerficiationEmail"
},
method: "POST",
beforeSend: function() {
$('#btnResendVerificationEmail').prop("disabled", true);
showLoading($('#btnResendVerificationEmail'));
},
success: function(data) {
clearInterval(int);
$('#btnResendVerificationEmail').html("Done");
console.log(data);
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btnResendVerificationEmail" class="btn btn-success">Send</button>
Read More: https://api.jquery.com/jquery.ajax/