Wait for a function to finish running another

Asked

Viewed 122 times

-2

I have on my page a form with a button called "Approve" at the end, which when triggered opens a container/field to be filled before approval, however, I want before opening this container/field, the form to be saved.

I have a Javascript function in my code that saves the form, and in this process, updates/reloads the page:

function salvarAprovar() {
    var $form = $('#formContrato');

    if (confirm("TESTE O contrato será salvo, deseja continuar?")) {
        if ($form.valid()) {
            document.formContrato.submit();              
        }
    }
}

In the current state of my code, the most I could was by clicking the "Approve" button, calling both functions, but even calling the salvarAprovar() First of all, it’s no use, because at the moment aprovarContrato() will be called, the new function is already updating/reloading the page.

How do I make my container/field launch function run only after the screen refresh/reload?

Code of the function that launches the container/field:

function aprovarContrato(codigo) {
    $('#modalContainer').load('Contrato/Aprovar/' + codigo, function () {
        $.applyDataMask();
        $(this).find('.modal').modal('show');
    });
}

How do I do for the function aprovarContrato() is only executed after the function salvarAprovar() has updated/reloaded the page?

I was recommended to use Ajax but did not know how to apply.

  • After the screen load you can use the $(document).ready(function), now "reload" you mean after submitting the form?

  • 3

    First of all you have to learn to use Jquery.Ajax, and when you learn to rephrase the question, because the way it is formulated the question of saving the code is secondary and whoever answers or would have to give you a lesson fold Ajax or put the code ready, which does not help to form knowledge.

  • I believe that without Ajax it is really complicated to manipulate these calls. You can use Axios if you do not want to use Ajax.

  • @Cypherpotato yes, when the form is sent the page reloads, as a F5

  • you have to learn Ajax in fact , knowing Promises and will be solved, Axios will be of great help as mentioned @Cypherpotato

1 answer

0

Browser other questions tagged

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