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

283
Vistas
React App freeze caused by function constantly running

This page of my react app contains the createBubbles function, which works ok. The problem is that when im going to another page after visiting this one, the whlole app freeze.

This is the errors that console shows, i tried with window.onloand but doesnt work.

 function createBubbles(){
    const section = document.querySelector('section');
    const createElement = document.createElement('span');
    var size = Math.random() * 60;

    createElement.style.width = size + 'px';
    createElement.style.height = size + 'px';
    createElement.style.left = Math.random() * innerWidth + 'px';
    window.onload=section.appendChild(createElement);
    setTimeout(() => {
      createElement.remove();
    },4000)
  }
  setInterval(createBubbles,50);

BrowserConsoleErrors

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

0

you need to clear your interval on component unmount.

const interval = setInterval(createBubbles,50);

useEffect(() => {
    return () => {
        clearInterval(interval)
    }
}, [])
about 4 years ago · Juan Pablo Isaza Denunciar

0

That error happens since when you unmount that component by navigating to another Component route, your Event Loop queue is still full of functions to be executed since the setInterval is still active, but the DOM node that you are selecting to append the bubbles no longer exists.

  • When using setTimeout, event listeners, setInterval, etc... in React, you need to place the execution inside a useEffect and you must make sure to clean the interval, event listener, timeout, etc... on unmount, you achieve that by returning from useEffect a function that performs the clear:

  • To use DOM nodes directly, don't use vanilla JS selectors but use a ref.

     export default function App() {
       const sectionRef = useRef();
       useEffect(() => {
         function createBubbles() {
         const section = sectionRef.current;
         const createElement = document.createElement('span');
         const size = getRandom(60);
         const innerWidth = window.innerWidth;
         const innerHeight = window.innerHeight;
    
         createElement.style.backgroundColor = 
         `rgba(${getRandom(255)},${getRandom(255)},${getRandom(255)})`;
         createElement.style.width = size + 'px';
         createElement.style.height = size + 'px';
         createElement.style.left = Math.random() * innerWidth + 'px';
         createElement.style.top = Math.random() * innerHeight + 'px';
    
         if (section) {
           section.appendChild(createElement);
    
           setTimeout(() => {
           createElement.remove();
           }, 4000);
           }
         }
         const interval = setInterval(createBubbles, 50);
         return () => clearInterval(interval);
         }, []);
         return <section ref={sectionRef}></section>;
       }
    
       function getRandom(max) {
         return Math.random() * max;
        }
    

A working example https://stackblitz.com/edit/react-959lni?file=src%2FApp.js

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