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

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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