Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Hacer que el bucle `for` espere a que se resuelva cada función

Página 1:

 function tempWindow(text) { var w = window.open('https://example.com/?data='+text, '_blank'); var t = setInterval(() => { if(w.closed) { clearInterval(t); return true; } }, 100); } var list = ['text1', 'text2', 'text3']; for(var i=0;i<list.length;i++) { tempWindow(list[i]);//Each loop iteration must wait for each window to close }

Ventana temporal:

 (function(){ setTimeout(() => { window.close(); }, 5000); })();

El código de la página 1 debería abrir tres ventanas y enviar a cada una de ellas las cadenas text1 , text2 , text3 , almacenadas en la variable data .

Estas ventanas no deben abrirse al mismo tiempo. Para ello, setInterval comprueba cada 100 milisegundos si la ventana anterior ya se ha cerrado.

Por lo tanto, la segunda ventana solo se puede abrir después de cerrar la primera y así sucesivamente.

Creo que la función tempWindow podría devolver una promesa y que usar await una solución sería posible, pero no sé cómo hacerlo.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Usa ese código. esperará hasta que la ventana esté cerrada

 function tempWindow(text) { return new Promise(resolve => { var w = window.open('https://example.com/?data=' + text, '_blank'); var t = setInterval(() => { if (w.closed) { clearInterval(t); resolve(true); } }, 100); }) } var list = ['text1', 'text2', 'text3']; for (var i = 0; i < list.length; i++) { await tempWindow(list[i]);//Each loop iteration must wait for each window to close }

asegúrese de que el código de bucle esté dentro de una función asíncrona como esa

 async testFunction(){ var list = ['text1', 'text2', 'text3']; for (var i = 0; i < list.length; i++) { await tempWindow(list[i]);//Each loop iteration must wait for each window to close } }

función de llamada como esta testFunction()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!