Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
asynchronous call to a confirm function with a handler function

I have a function I found online and modified which displays a prompt, and executes a handler depending on the response of the user:

This function waits for the user to submit a response and executes the function accordingly.

function confirmDialog(message, handler, options = ["Yes", "No"]) {
    $(`<div class="modal fade" id="confirmDialog" role="dialog"> 
     <div class="modal-dialog"> 
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Confirmation</h5>
            </div> 
            <div class="modal-body" style="padding:10px;"> 
                <p class="text-center">${message}</p> 
            </div>
             <div class="modal-footer text-center"> 
               <a class="btn btn-primary btn-yes">${options[0]}</a> 
               <a class="btn btn-secondary btn-no">${options[1]}</a> 
             </div> 
          </div> 
       </div> 
    </div> 
  </div>`).appendTo('body')

    //Trigger the modal
    $("#confirmDialog").modal({
        backdrop: 'static',
        keyboard: false
    })

    //Pass true to a callback function
    $(".btn-yes").click(function () {
        handler(true);
        $("#confirmDialog").modal("hide")
    })

    //Pass false to callback function
    $(".btn-no").click(function () {
        handler(false)
        $("#confirmDialog").modal("hide");
    })

    //Remove the modal once it is closed.
    $("#confirmDialog").on('hidden.bs.modal', function () {
        $("#confirmDialog").remove()
        $("#confirmDialog .btn-yes").prop("disabled", true)
    })
    $("#confirmDialog").on('show.bs.modal', function () {
        $("#confirmDialog .btn-yes").prop("disabled", true)
    })
}

The problem is that I want to wait for the user response, and the handler to execute before executing the next line of code. For example:

async function submit(){
  disableInput()  
  confirmDialog("Do you want to submit?", (userPressedYes) => {
         if(userPressedYes) {
           doSubmit(); //uses $.post()
          }
  }
  resetInput()
}

In the above example the resetInput() function executes without waiting for the input of the user. I need it to wait for a response, and potentially the handler to execute before resetting the input.

Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda