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

207
Views
¿Por qué clearInterval solo funciona la primera vez?

Este es mi código a continuación:

 function triggerTimer() { var myInt = window.setInterval(timerStart, 1000); setTimeout(removeInt, 4000); } function timerStart() { var timerValue = parseInt(document.getElementById('timer').innerHTML); if (timerValue > 0) { timerValue -= 1; document.getElementById('timer').innerHTML = '0' +timerValue.toString(); console.log(timerValue); } else { var correct = document.getElementById(`c${row}`).innerHTML; multiplechoice.innerHTML = '<p>The correct answer is <span style="color: white;" id=`c${row}`>'+correct+'</span><br><button onclick="submitEntry();">Next Question</button></p>'; } } function removeInt() { if(clearInterval(myInt)) {console.log('success')} else {console.log('fail')} }

Encima de este código hay un temporizador que disminuye cada vez que se llama a la función timerStart. El problema es que el temporizador se detiene la primera vez que se activa la función removeInt, aunque registra un "fallo".

Pero la segunda vez que se activa timerStart, el tiempo sigue disminuyendo, pero cuando se llama a removeInt, el intervalo no se borra.

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

0

Se está registrando un failed porque clearInterval devuelve indefinido incluso cuando tiene éxito.

Entonces su función removeInt ,

 if(clearInterval(myInt)) {console.log('success')} else {console.log('fail')}

está siendo evaluado así,

 if(undefined) {console.log('success')} else {console.log('fail')}

Aquí hay un documento sobre clearInterval para obtener más información, https://developer.mozilla.org/en-US/docs/Web/API/clearInterval

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!