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

193
Visualizações
How can I loop some code so that it repeats every time confirm() returns true?

I've tried using do-while loops but it doesn't seem to be working properly:

let rep; //this variable tells the loop whether to run or not
let nOfTimesTheLoopRuns = 0;

do {
    nOfTimesTheLoopRuns++;
    console.log(`This loop has run ${nOfTimesTheLoopRuns} time(s).`);

    setTimeout( () => {
        rep = confirm("Repeat?");
    }, 2000); //a delay is set so that the answer can be printed on the console before the code runs again
} while (rep);

The console prints: "This loop has run 1 time(s).", but it doesn't repeat as it should when I press "Ok" in the confirm(); dialog box.

I've also tried this:

let rep = []; //this variable tells the loop whether to run or not
let nOfTimesTheLoopRuns = 0;

do {
    rep.pop();

    nOfTimesTheLoopRuns++;
    console.log(`This loop has run ${nOfTimesTheLoopRuns} time(s).`);

    setTimeout( () => {
        rep.push(confirm("Repeat?"));
    }, 2000); //a delay is set so that the answer can be printed on the console before the code runs again
} while (rep[0]);

In the end, the console prints "This loop has run 1 time(s)." And the value of nOfTimesTheLoopRuns is 1. How can I make it so that it keeps running every time the user presses "Ok" in the confirm(); dialog box?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can do put the code you want to execute each time the user confirms into a function, then check whether rep is true in the setTimeout callback and call the function again if so:

let nOfTimesTheLoopRuns = 0;

function check() {
  nOfTimesTheLoopRuns++;
  console.log(`This loop has run ${nOfTimesTheLoopRuns} time(s).`);
  setTimeout(() => {
    if (confirm("Repeat?")) {
      check()
    }
  }, 2000)
}


check()

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a function that calls itself if answer is true.

let nOfTimesTheLoopRuns = 0;
function test() {
  if (confirm("Repeat") === true) {
    nOfTimesTheLoopRuns++;
    console.log(`This loop has run ${nOfTimesTheLoopRuns} time(s).`);
    setTimeout(() => test(), 2000);
  }
}
test();

about 4 years ago · Juan Pablo Isaza Relatório

0

This is because the setTimeout would run after the completion of the loop, that's how the javascript handles asynchronous functions. You can better understand this by reading concept of Event loop

What you can do is to put all your code in an interval and clear it when user selects 'cancel'.

var nOfTimesTheLoopRuns = 0;
var myInterval = setInterval(function(){
    nOfTimesTheLoopRuns++;
    console.log(`This loop has run ${nOfTimesTheLoopRuns} time(s).`);
    if(!confirm("Repeat?")){
       clearInterval(myInterval);
    }
}, 3000);
about 4 years ago · Juan Pablo Isaza Relatório
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