Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!