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

108
Visualizações
AJAX, JS, setInterval. Displaying data multiple times

Hello and thank you for reading my post

I have created a basic piece of AJAX. It looks like the following:

    <div id="name"></div>

    <script>
        async function get_name() {
            let server = await fetch("pageOneName")
            let data = await server.text()
            document.querySelector("#name").innerText = data
        }

        setInterval(get_name, 3000)
    </script>

as of now it doesn't really do much but retrieve my first name from the file/url

What I would like to do is...

Display my name multiple times (max 5 times) in multiple rows. One name after 3000, another after 4000, etc.

Like.

  • Michael Anderson
  • Michael Anderson
  • Michael Anderson

What would my approach be here?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You just need to create an interval of 3000, 4000, 5000 to create the name every second after 3 seconds and then create a counter to count the limit, when the limit is reached then clear the intervals.

// initialize the variables.
var counter = 0;
var intervals = [];

// clear all intervals
function clearAllIntervals() {
    for (var id of intervals) {
        clearInterval(id);
    }
}

async function get_name() {
    let server = await fetch("pageOneName");
    let data = await server.text();

    let p = document.createElement("p");
    p.append(data);
    document.querySelector("#name").append(p);
    counter +=1;
    
    // Clear the counter and the intervals show 5 names.
    if (counter === 5){
        clearAllIntervals();
        counter = 0;    
    }
}

intervals.push(setInterval(get_name, 3000));
intervals.push(setInterval(get_name, 4000));
intervals.push(setInterval(get_name, 5000));
<html> 
<body>
  <div id="name"></div>
</body>
</html>

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