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.
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