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

168
Visualizações
In the browser console, open the url in a new tab with a delay

I have an array of URLs and I want to open all these URLs one by one in the new tab with the specified delay. I have tried the below method, but it opens all URLs in a one-shot after a delay of 2 seconds. Please suggest the code changes.


items = ["a.com", "b.com", "c.com", "d.com"]

items.forEach((url, index) => {
    if (index <= 49) {
        setTimeout(() => {  window.open(url, "_blank"); }, 2000);
    }
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use async function for that.

Sample Implementation.

items = ["a.com", "b.com", "c.com", "d.com"];

function resolveAfter2Seconds(index) {
  return new Promise(resolve => {
    setTimeout(() => {
      window.open(items[index], "_blank")
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  let index = 0;
  while (index < items.length) {
    const result = await resolveAfter2Seconds(index);
    console.log(result);
    // expected output: "resolved"
    index++;
  }
}

asyncCall();

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