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);
CodePudding user response:
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.