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

182
Vistas
How can I use setInterval and clearInterval in two different function?

What am I missing here? I can call myInterval function, but I can't stop it with clearInterval.

const getData = () => {
db.transaction((tx) => {
  tx.executeSql(
    "SELECT * FROM itinerary_details",
    [],
    (tx, results) => {
      var data = [];
      for (let i = 0; i < results.rows.length; ++i)
        data.push(results.rows.item(i));
      console.log(data) 
    }
  )
})
}

So I am trying to have 2 buttons with 2 different function that I can call:

to start:

const myInterval = () => {
   setInterval(getData, 5000)
}

to end the setInterval:

const stopData = () => {
   clearInterval(myInterval)
   console.log("STOPED")
}

I can see the console.log, but not calling the function myInterval

Please help.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to store the interval ID somwhere and when clearing, you need this one.

let intervalID = null; // global

const
    startData = () => {
        if (intervalID !== null) return;
        intervalID = setInterval(getData, 5000);
    },
    stopData = () => {
        if (intervalID === null) return;
        clearInterval(intervalID);
        intervalID = null;
        console.log("STOPED");
    },
    getData = () => console.log(new Date().toISOString() + ': getData');

document.getElementById('start').addEventListener('click', startData);
document.getElementById('stop').addEventListener('click', stopData);
<button id="start">Start</button> <button id="stop">Stop</button>

about 4 years ago · Santiago Trujillo 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