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

131
Vistas
When using a setInterval , Switch statement function returning undefined every x ms

i'm asking your help because i have issues with a function. I'm trying to generate a random animation, and the solution i found is using a switch state with a Math.random() to match every case and returning a string that match the animation in my other Table.

Here's the code, so maybe it'll be more understanable

function PickAnimation () {
    
    let value = Math.floor(Math.random() * 4);

    const animationList = ['idle', 'attack', 'walk']

    switch(value) {
        
        
        case 1:
            return result = animationList[0];
        
        case 2:
            return result = animationList[1];

        case 3:
            return result = animationList[2];

        default:
            return result = animationList[0];
        
    }
}
setInterval(PickAnimation(), 1000)

The issue is that it is actually working for the first animation which is random as expected, but then when i set a set interval, it says that for example 'idle is undefined'

I've search for solution everywhere but did not find any working solutions.

I'll be glad to have some answers about this, thanks !

about 4 years ago · Santiago Gelvez
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