Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

70
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!