Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda