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

223
Views
¿Cómo detener la llamada setTimeout anidada después de un tiempo fijo?

Tengo este fragmento de código:

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

Esto crea un bucle infinito que no termina. ¿Cómo puedo detener este setTimeout después de suponer 10000 ms o 10 s?

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

0

Una solución es empujar el tiempo de timeout a la matriz cada vez que lo crea y recorrer la matriz para borrarlo.

 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)

Pero, siempre es estable usar 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!