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

95
Vistas
React double values, useEffect and setInterval

Why do I have double values? I am using useEffect and the setInterval function. In the setInterval function there is a single increment or decrement and after a second there is another one. Why am I getting a double value in the results? How to change it?

  const demo = "1 2 3 4 5";
  let i = 0;  
  let j = demo.length;
  useEffect(() => {
    const interval = setInterval(() => {
      if (i < demo.length) {
        console.log(demo.substring(0, i));
        i++;
      } else if (j > 0) {
        console.log(demo.substring(0, j));
        j--;
      }
    }, 1000);
  }, [i, j]);

Result:

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

0

You are not getting double values, you are getting the spaces in the demo string.

You results is

'1'
'1 '
'1 2'
'1 2 '
'1 2 3'
'1 2 3 '
'1 2 3 4'
'1 2 3 4 '
'1 2 3 4 5'
'1 2 3 4 '
'1 2 3 4'
'1 2 3 '
'1 2 3'
'1 2 '
'1 2'
'1 '
'1'
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