Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
clearInterval does not clear setInterval inside function

How do I clear interval for the setInterval inside the StartGame function ?(present one is not working)

document.getElementById("rndletter").addEventListener("click", function StartGame() ** {
  setInterval(function() ** {
    var result0 = String.fromCharCode(Math.floor(Math.random() * (122 - 97)) + 97);
    var result1 = String.fromCharCode(Math.floor(Math.random() * (122 - 97)) + 97);
    var result2 = String.fromCharCode(Math.floor(Math.random() * (122 - 97)) + 97);
  }, 2000);
});
document.getElementById("stprndletter").addEventListener("click", function() {
  clearInterval();
});
<button id="rndletter">Start game</button>
<button id="stprndletter">Stop game</button>
<td style="text-align: center; color: blue; font-size: 150%;">
  <b>
        <div id="result0"><br />
        </div>
    </b></td>
<td style="text-align: center; color: brown; font-size: 150%;">
  <b>
        <div id="result1"><br /></div>
    </b></td>
<td style="text-align: center; color: hotpink; font-size: 150%;"><b>
        <div id="result2"><br /></div>
    </b></td>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I got the answer after reading the docs, for anyone who searches the same, I have done this: (added an ID nIntervID)

let nIntervId;

document.getElementById("rndletter").addEventListener("click", function {
    nIntervId = setInterval(function() {
        var result0 = String.fromCharCode(Math.floor(Math.random() * (122 - 97)) + 97);
        var result1 = String.fromCharCode(Math.floor(Math.random() * (122 - 97)) + 97);     
        var result2 = String.fromCharCode(Math.floor(Math.random() * (122 - 97)) + 97);

        document.getElementById("result0").innerText = result0;
        document.getElementById("result1").innerText = result1;
        document.getElementById("result2").innerText = result2;
    },2000);});
    document.getElementById("stprndletter").addEventListener("click", function() {
        clearInterval(nIntervId);
        // release our intervalID from the variable
        nIntervId = null; 
    });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda