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

147
Vistas
Make the `for` loop wait for each function to resolve

Page 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
}

Temporary window:

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

The code on page 1 should open three windows and send to each of them the strings text1, text2, text3, stored in the variable data.

These windows must not be opened at the same time. For this, setInterval checks every 100 milliseconds if the previous window has already been closed.

Therefore, the second window can only be opened after closing the first one and so on.

I believe that the tempWindow function could return a promise and that using await a solution would be possible, but I don't know how to do that.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

use that code. it will wait until window is close

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
}

make sure loop code is inside async function like that

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
}
}

call function like this testFunction()

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