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

478
Vistas
Reaccionar clearInterval no funciona en el componente de clase

Estoy tratando de hacer un temporizador que comience al comienzo de la visualización de clasificación y se ejecute hasta que finalice la clasificación. El temporizador se inicia en consecuencia, pero no se detiene después de la clasificación. esto funciona absolutamente bien en Componente funcional (con gancho useEffect) pero no funciona en Componente de clase.

aquí está mi función startTimer -

 startTimer = () => { let isAlgo = this.state.isAlgorithmSortOver; let interval; if (isAlgo === true) { interval = setInterval(() => { this.setState({ time: this.state.time + 10, }); }, 10); } else if (isAlgo === false) { clearInterval(interval); } // return () => clearInterval(interval); };

y aquí está la función startVisualizer -

 startVisualizer = () => { let steps = this.state.arraySteps; let colorSteps = this.state.colorSteps; this.clearTimeouts(); let timeouts = []; let i = 0; while (i < steps.length - this.state.currentStep) { let timeout = setTimeout(() => { let currentStep = this.state.currentStep; this.setState({ array: steps[currentStep], colorElement: colorSteps[currentStep], currentStep: currentStep + 1, isAlgorithmSortOver: false, }); //? comparing the currentStep with arraySteps and the state of isAlgorithmSortOver will remain false until the array is fully sorted.. Adding '+ 1' to currentStep because the arraySteps state always will be '+1' bigger than the currentStep.. if (currentStep + 1 === i) { this.setState({ isAlgorithmSortOver: true, }); } timeouts.push(timeout); }, this.state.delayAnimation * i); i++; } this.startTimer(); this.setState({ timeouts: timeouts, // isAlgorithmSortOver: false, }); };
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

porque no estás limpiando el intervalo. Intente guardar la identificación del intervalo para indicar así:

 startTimer = () => { let isAlgo = this.state.isAlgorithmSortOver; if (isAlgo === true) { interval = setInterval(() => { this.setState({ time: this.state.time + 10, }); }, 10); this.setState({interval}) } };

Luego puede llamar a clearInterval donde quiera, así:

 clearInterval(this.state.interval)

También debe sacar timeouts.push(timeout) de setTimeout donde in while . Porque, de lo contrario, timeouts.push(timeout) no funciona sincrónicamente, funciona después de un tiempo.

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