It seems that if you use a modal to cover a screen, like in the uploaded example in Post Disabling a form, while the app waits for a critical response from the server, you can have problems programmatically removing the modal.
You say really? Yes. Because the modal doesn’t finish rendering for quite some time after you ask to show it. And the same rendering delay happens on hide as well. So if you show, then almost immediately hide, as the server just responded, then the hide gets lost. And you are stuck with the modal and no escape. Or the reverse, the show gets reversed, and the user is pushing more buttons you aren’t ready to handle.
The solution is to trap the rendering completed events for the modal show and hide, and keep track of the current state of the modal, if it’s rendering or not, and what is the next state once the rendering has completed, if any. Here are the Toggle function and event handlers I’m using:
$("#WaitModal").on('shown.bs.modal', function(){
ModalIsShown = true;
ModalIsRendering = false;
ToggleModal(NextModalState);
});
$("#WaitModal").on('hidden.bs.modal', function(){
ModalIsShown = false;
ModalIsRendering = false;
ToggleModal(NextModalState);
});
function ToggleModal(ShowModal) { // ShowModal Boolean: True mean show
if (ModalIsRendering) {
NextModalState = ShowModal;
} else {
if (ModalIsShown !== ShowModal) {
if (ShowModal) {
$('#WaitModal').modal("show");
ModalIsShown = true;
} else {
$('#WaitModal').modal("hide");
ModalIsShown = false;
};
ModalIsRendering = true;
};
};
};