Pause a for to execute a request

Asked

Viewed 59 times

0

I am mounting a questionnaire where each question should appear one at a time on the screen for the user and, as soon as the user answers it, he should proceed to the next question.

For this I am using a for to go from question to question and a function to assemble the question with the possible answers as code below:

for (let aux = 1; aux < nrPerguntas + 1; aux ++) {
    montaQuestionario(aux)
}
function montaQuestionario(aux) {
    pPergunta.innerHTML =  perguntas(aux)
    pEscala.innerHTML = ""
    for ( var i= 0; i < respostas.length ; i++) {
        pEscala.innerHTML = pEscala.innerHTML + `<td class="tdBorda" onclick="#"> ${respostas[i][1]} </td><td class="tdBranco"></td>`
    }
}

My problem is that the for goes from the first to the last question without pausing when it goes to the mount functionQuestionario() and if you break it stops running.

Is there any way to make it wait for a return or action from the user to continue? This action would be within the function since it would be there that the user would make the response selection.

  • Ricardo, it is not very clear in the question you want. If you give an edited believe that it is easier to get answer.

  • Good morning, I rewrote the doubt, whether it’s clearer now.

1 answer

0

I believe that an easier way to do this would be to create functions that would essentially act as a FOR but would only be called when needed.

    const botao_de_resposta = document.getElementById("idBotao");
    botao_de_resposta.addEventListener("click",responderPergunta);
    
    aux = 0;
    proximaPergunta(a)

    function responderPergunta() {
        //Logica para ver se a resposta esta certa
        //...

        aux++
        proximaPergunta(aux)
    }
    function proximaPergunta(a){
        pPergunta.innerHTML =  perguntas(a)
        //Poe a pergunta na tela
        //...
    }

In that case he would check if the question was answered and move on to the next one when you click on the reply button, which I assume you have in your HTML.

Browser other questions tagged

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