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
EventListener execute order

I have simple code like follow:

setInterval(() => {
  window.scrollTo(x, y);
  // some simple logic (like console.log) goes here
},1000);
window.addEventListener('scroll', () => {
  console.log('scroll');
});

As i expect, event handler execute immediately after window.scrollTo(), but sometimes "some simple logic" execute earlier than scroll handler. Why?

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

0

The order of execution is not as it is in the code (chronologically). The event listener is triggered on the scroll event (part of the JS even loop mechanism) and the setInterval async function executes each second. JS does not support multi-threading, so I guess the processes are ordered in some kind of queue, and one of the executions (setInterval function executes on a strict time interval, whereas the eventListener executes when it has been triggered).

Maybe try reconstruct the logic to avoid this effect You can use this as a reference here

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