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

191
Views
Animate text letter by letter with requestAnimationFrame()

I would like to animate text from the DOM letter by letter using requestAnimationFrame(). I have achieved it with setInterval() and also with requestAnimationFrame() too, but I still can't achieve it to be played for predefined time. For example, on my machine every frame plays for 6ms, but I would like the whole text to be played for example for 5s. So if I have 100symbols each symbol should be added for 50ms and then the other one. Could you please recommend me a solution? Here is my code:

const text = document.getElementById('text');
const textContent = text.innerText;
text.innerHTML = '';
let i = 0;
let totalTime = 2000;
let start;

function animateText(time) {
    if (start === undefined) {
        start = time;
    }

    if(text.innerHTML.length < textContent.length) {
             requestAnimationFrame(animateText);
             text.innerHTML += textContent[i];
             i++;
    }
}

requestAnimationFrame(animateText);
about 4 years ago · Juan Pablo Isaza
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!