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

73
Vistas
Animate text with array

I'm trying to animate text with javascript using an array with time intervals e.g. t te tex text, I want it to cycle from 0 to 21 and back and I'm not entirely sure how to do it. Any help is muchly appreciated!

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

0

General concept:
You need to update string output each tick with next value till full string will be visible. Tick interval defines how fast animation is.

One of possible realization below:

// Wait [ms] milliseconds
const Delay = ms => new Promise(r => setTimeout(r, ms));

// Main function
const main = async () => {
  const p = document.querySelector('p');
  // String you wanna animate
  const targetText = 'Hello there!';
  // Tick interval
  const tick = 60;
  
  // Iterating through string
  for (let i = 1; i <= targetText.length; i++) {
    // Set new value 
    p.innerHTML = ('&nbsp;'.repeat(targetText.length - i) + targetText.slice(-i));
    // Wait for a next tick
    await Delay(tick);
  }
}

main();
p {
  font-family: Monospace;
}
<p></p>

Here are:
&nbsp; - space will be visible inside of .innerHTML(); tick - interval between animation frames (in milliseconds);

Refs: .slice(), .repeat().

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