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

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

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