Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda