Modal only closes when receiving message

Asked

Viewed 47 times

-1

have this code below. When the user finishes the form he closes the class modal ". esqFecha". and opens the loading modal. However, the "esqfecha" modal only closes when the message is sent and the "loading" modal closes before the "esqFecha" modal. How could I do to the modal "loading" close after the modal "esqFecha" receive the message

$("#formEsq").on("submit", function (event) {
   event.preventDefault();
   $("#loadMe").modal({
      backdrop: "static", //remove ability to close modal with click
      keyboard: false, //remove option to close with keyboard
      show: true //Display loader!
   });
   setTimeout(function () {
      $("#loadMe").modal("hide");}, 3500);

   var enderecoesq = jQuery('.enderecoesq').attr("data-enderecoesq");
console.log(enderecoesq);
   $.ajax({
      method: "POST",
      url: enderecoesq,
      data: new FormData(this),
      contentType: false,
      processData: false,
      success: function (retorna) {
         if (retorna['erro']) {
            $('#msgEsq').html(retorna['msg']);
            $('#msgCad').html(retorna['msg']);
            $('.esqFecha').modal('hide');
            $('#loginModal').modal('show');
         } else {
            $('#msgEsq').html(retorna['msg']);
         }
      }
});
  • What would be "modal receive the message"? You should not use the callback beforeSend to display the loading?

  • when he finishes the registration in the database he returns the registered message and then closes the modal and goes to login. or registered keeps in modal,

1 answer

-1


You have to close the loading modal within the AJAX callback, the function block of the success. Using setTimeout does not guarantee that the time the modal will close will be synchronized with the time of the AJAX request.

Remove setTimeout and enter the code $("#loadMe").modal("hide"); within the success:

$("#formEsq").on("submit", function (event) {
   event.preventDefault();
   $("#loadMe").modal({
      backdrop: "static", //remove ability to close modal with click
      keyboard: false, //remove option to close with keyboard
      show: true //Display loader!
   });

   var enderecoesq = jQuery('.enderecoesq').attr("data-enderecoesq");
   $.ajax({
      method: "POST",
      url: enderecoesq,
      data: new FormData(this),
      contentType: false,
      processData: false,
      success: function (retorna) {
         $("#loadMe").modal("hide"); // FECHA O LOADING
         if (retorna['erro']) {
            $('#msgEsq').html(retorna['msg']);
            $('#msgCad').html(retorna['msg']);
            $('.esqFecha').modal('hide');
            $('#loginModal').modal('show');
         } else {
            $('#msgEsq').html(retorna['msg']);
         }
      }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.