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

242
Views
clearInterval in loop don't work as expected

I have a problem When I delete the letters, the word remains

I have two h1 I delete the word from each h1 and when all the letters are deleted I stop the interval for each h1 specially for it

img

let mySelector = document.querySelectorAll(".writ-text");
for (let l = 0; l < mySelector.length; l++) {

  removeText = setInterval(function() {

    // Cut the last letter of the word and print the word after editing
    document.querySelectorAll(".writ-text")[l].textContent =
      document.querySelectorAll(".writ-text")[l].textContent.substr(0,
        document.querySelectorAll(".writ-text")[l].textContent.length - 1);

    // Check if the entire word has been deleted
    if (document.querySelectorAll(".writ-text")[l].textContent.length == 0) {
      clearInterval(removeText)
    }
  }, 1000);
}
<h1 class="writ-text">gold</h1>
<h1 class="writ-text">golder</h1>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have cleared the only setInterval named removeText(global scoped) which makes it for both text.
Use a block scope like let const to work out

let mySelector = document.querySelectorAll(".writ-text");
for (let l = 0; l < mySelector.length; l++) {

 let removeText = setInterval(function() {

    // Cut the last letter of the word and print the word after editing
    document.querySelectorAll(".writ-text")[l].textContent =
      document.querySelectorAll(".writ-text")[l].textContent.substr(0,
        document.querySelectorAll(".writ-text")[l].textContent.length - 1);

    // Check if the entire word has been deleted
    if (document.querySelectorAll(".writ-text")[l].textContent.length == 0) {
      clearInterval(removeText)
    }
  }, 1000);
}
<h1 class="writ-text">gold</h1>
<h1 class="writ-text">golder</h1>

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!