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

107
Vistas
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 Respuestas
Responde la pregunta

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