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

213
Views
How to stop nested setTimeout call after a fixed time?

I have this piece of code:

let timerId = setTimeout(function tick() {
   alert("tick");
   setTimeout(tick(), 1000);
 }, 1000);

This creates an infinite loop that doesn't end. How can I stop this setTimeout after suppose 10000 ms or 10 s?

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

0

A soluation is to push the timeout to array when everytime you create it and loop over the array to clear it.

    let arr = []
    let timerId = setTimeout(function tick() {
       console.log("tick");
       arr.push(setTimeout(tick(), 1000));
     }, 1000);


    setTimeout(function(){
    arr.forEach(i=>{
     window.clearTimeout(i)
     }))
     },10000)

But, it always stable to use setInterval():

let arr = []
setInterval(function tick() {
   console.log("tick");
 }, 1000);

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!