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

144
Visualizações
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 Respostas
Responde à pergunta

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