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

108
Views
SetTimeout not doing its job

I've seen a few questions that look similar to this, but nothing so far that seems to work in my situation. I made a little typing function - it's meant to loop through a series of phrases passed into the element's html attribute, and "type" them out, as if somewhere were typing and erasing each letter one by one.

The HTML, when rendered (it's written in PHP), looks something like this:

<h1>Constant text: <span data-text="Example 1, Example 2, Example 3">Changing Text</span></h1>

The "Changing Text" gets "typed out" and then "erased".

The "typing" function recursively adds letters to the element, one by one, from a string parameter, and the "erasing" function remove letters recursively:

function type (el, letters, delay = 0) {
    if ( letters.length < 1 ) {
        return;
    }

    var letter = letters.shift(),
        html   = el.text();

    el.text(html + letter);

    setTimeout(type(el, letters, delay += 200), delay);
}

function erase (el, delay = 0) {
    if ( el.html().length < 1 ) {
        return;
    }

    var text = el.text();

    el.text(text.substring(0, text.length - 1))

    setTimeout(erase(el, delay += 200), delay);
}

Both functions seem to be working, when debugging with console log. But for some reason, the setTimeout isn't setting a timeout... The letters in each phrase appear and disappear simultaneously...

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!