I have 2 nested AJAX requests in my code. They are fired when a button is pressed. I'm using Bootstrap's loading stages to change the button's text to "Loading…" while the AJAX requests are being processed.
The issue is that the button gets reset before the inner AJAX request has completed.
My expectation is that the complete
callback of the outer AJAX request would be executed only after all the code in its success
callback has been executed, but that's not what's happening.
The code's simplified version looks like this:
$('#button').on('click', function() {
$.ajax({
url: …,
type: 'post',
data: …,
dataType: 'json',
beforeSend: function() {
$('#button').button('loading');
},
complete: function() {
$('#button').button('reset');
},
success: function(json) {
if (json['error']) {
…
} else {
$.ajax({
url: …,
dataType: 'json',
success: function(json) {
for (var delayer = 1; delayer < 2000000000; delayer++) {}
…
},
error: …
});
}
},
error: …
});
});
I've created the arbitrary delaying loop to be able to better test the effect on my machine. It gives me about 2 seconds of delay. The button resets almost instantly though.
Is that how it's supposed to work and if it's not – how can I fix it?