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

116
Vistas
clearIntervall don't stop scrolling set with timer

I'm a js newbie but from what I read, using clearInterval with id returned previously by setInterval should reset the timer.

On a pedal push I receive a midi event with a positive value and then same event with a zero value on pedal release.
Using the code below, I can see my page turning red on release but the page keeps scrolling. Any idea why ?

function handleMIDIMessage(event) {
  var scroll_id;
  if (event.data.length === 3) {
    if (event.data[0] == 176 && event.data[1] === 67) {
      if (event.data[2] > 0) {
        scroll_id = setInterval(function() {
          window.scrollBy({
            top: 50,
            behaviour: "smooth"
          });
        }, 1000);
        document.body.style.background = 'green';
      } else {
        document.body.style.background = 'red';
        clearInterval(scroll_id);
      }
    }
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to declare the interval outside so you do not keep creating a new variable and losing the id. You should probably also check to make sure you are not creating more than one interval.

//declare it so it is not overwritten
var scroll_id;
function handleMIDIMessage(event) {

  if (event.data.length === 3) {
    if (event.data[0] == 176 && event.data[1] === 67) {
      if (event.data[2] > 0) {
        // if we were defined before, cancel the last one
        if (scroll_id) window.clearInterval(scroll_id);
        scroll_id = setInterval(function() {
          window.scrollBy({
            top: 50,
            behaviour: "smooth"
          });
        }, 1000);
        document.body.style.background = 'green';
      } else {
        document.body.style.background = 'red';
        clearInterval(scroll_id);
      }
    }
  }
}
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