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

176
Views
How to create dynamically variable for setInterval?

I create a hmtl page with node and ejs with an unforseeable number of elements. I would like to create a setInterval for some, none or all of those elements, depending what the user is doing.

The problem is, that I am not able to create a dynamically variable for the setInterval so that I can later on cancel those Intervals.

Maybe I just need another simpler approach but at the moment I am stuck here.

camContainer.forEach(element => {
        clearInterval(intervalVar);
        if (!element.classList.contains("hidden")) {
            countVisible++;
            intervalVar = setInterval(showConsole, 1500);
        } else {
            countHidden ++;
        }
    count++;    
    })

I tried it with an Array instead of a regular variable but that didnt work either

intervalVar[count] = setInterval(showConsole, 1500);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're on the right track with an array, but you need to push items onto it:

intervalVar = []

// ...

intervalVar.push(setInterval(showConsole, 1500))

When you want to cancel an interval, remove it from the array with slice or pop, depending on how you're selecting the item to cancel.

about 4 years ago · Juan Pablo Isaza Report
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!