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

120
Vistas
Clearing setTimeouts that are in conditional blocks

I have timeouts that only initialize when the page is scrolled a certain amount. The problem being that if I scroll to a page and don't wait for the timeout it continues to run into the next page. So I want to clear them when that page is no longer at that scroll offset. I found that you can only clear timeouts by storing them in a variable (ie: let x = setTimeout(func,time)) and then clearing the variable. But if I declare them inside the block statements the other blocks can't access it. But if I make it global it initializes without any condition. When I try encasing the global in a function so it doesn't execute right away (let x = ()=>{setTimeout(func,time)}), then I can no longer clear it for some reason.

contain.addEventListener("scroll",()=>{
// Frame 1
if(contain.scrollTop == 0){
    handleAudio2();
    handleAudio3();
    handleAudio4();
    emptyParticlesTwo();
    vid.pause();
}
if(contain.scrollTop == window.innerHeight){
    setTimeout(()=>{
        contain.scrollBy(0,window.innerHeight)},23000)
}
if(contain.scrollTop == window.innerHeight*2){
    setTimeout(()=>{
        contain.scrollBy(0,window.innerHeight)},23000)
}})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Just save the timeout id in a variable that is in a scope outside of the if statements. I would also recommend combining your two if statements, or putting an else before the second one. The way you have posted them here, the 2nd is redundant (it looks like it the 2nd 'if' shouldn't be in the timeout function)

var toID;
if(contain.scrollTop == window.innerHeight){
    toID = setTimeout(()=>{
        contain.scrollBy(0,window.innerHeight)},23000)
}
if(contain.scrollTop == window.innerHeight*2){
    toID = setTimeout(()=>{
        contain.scrollBy(0,window.innerHeight)},23000)
}})
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