Hi folks!
I want to nest multiple setInterval functions within a parent function and then set that parent function to infinite, so that the entire function, within loops infinitely. Is that possible? Thanks in advance for your help!
So far, I created a JavaScript animation that consists of 3 different setInterval
functions. How do I get the entire thing to repeat infinitely? See JSFiddle here: https://jsfiddle.net/MenelikMakonnen/ebs2m0rq/
See javascript code here:
$(document).ready(function() {
setInterval(function(){
$(".red-one").addClass("red-one-glow");
},300);
setInterval(function(){
$(".red-two").addClass("red-two-glow");
},200);
setInterval(function(){
$(".red-three").addClass("red-three-glow");
},100);
});
After some research on Stack Overflow, I came up with this addition to m code:
$(document).ready(function() {
function animateAll() {
var listItems = $('.red').length;
var count = 0;
setInterval(function(){
$(".red-one").addClass("red-one-glow");
},300);
setInterval(function(){
$(".red-two").addClass("red-two-glow");
},200);
setInterval(function(){
$(".red-three").addClass("red-three-glow");
},100);
count += 1;
if (count >= listItems) {
count = 0;
}
animateAll();
}
});
But this doesnât work! What Iâm I doing wrong?
Well, you are not nesting those. They are just interpreted sequentially and then each interval runs concurrently.
The other problem is that you are adding the classes without removing the old ones. When it sees that that class is already there it doesnât do anything, so whatever class was successfully added last will win. You can remove specific classes with removeClass or if you send it no parameters it will just remove them all.
For this reason, I wouldnât use class to target the jQuery selector if I could avoid it.
Here is a pen with a simple example. It starts an interval of 1s to change the background color. Then if waits 500ms and starts another interval to do the opposite - this way they are 500ms out of phase.
$(document).ready(function() {
setInterval(function() {
$("#hi").removeClass("blue").addClass("green");
}, 1000);
setTimeout(function() {
setInterval(function() {
$("#hi").removeClass("green").addClass("blue");
}, 1000);
}, 500);
});
Hey @kevinSmith, for this same example that you posted, how would you make it run infinitely? Also, you are correct, those setInterval
functions that I posted were not nested, but my goal was to house all those setInterval
functions (or nest them) in one function, and then set that function to run infinitely. Basically I want that entire group of animated three rings to run infinitely. Hope my logic isnât off lolâŚ
I donât understand âmake it run infinitelyâ - it will run indefinitely. the setInterval function keeps running until either the program stops running or you manually clear it. It will never stop. If I need to stop it, I can do something like:
const myInterval = setInterval(function() ...
and then later if I want it to stop:
clearInterval(myInterval)
Do the changing colors in my example stop? They should switch every 500ms until the program stops.
âI donât understand âmake it run infinitelyâ - it will run indefinitely.ââŚyeah thatâs what I mean, indefinitely lol!
And yes, after awhile, the changing colors in your program does stop. Maybe like after a couple minutes or so.
Also, in this statement, âthey should switch every 500ms until the program stopsâ, what do you mean by âuntil the program stopsâ? Sorry for my misunderstanding. I consider personally, that I shouldnât get confused on this kind of simple stuff, but unfortunately after hours of coding, my brain is almost friedâŚ
Also, this is an example of the type of indefinite function that Iâm talking about. https://jsfiddle.net/MenelikMakonnen/4v2drbao/ (fork). This was my original inspiration. Thanks for all the help man!
what do you mean by âuntil the program stopsâ?
I mean until the program stops - you close that browser window, your computer crashes, thereâs a power outage, a biblical flood, alien lizard men attack and set off an EMP and shut down the electrical grid ⌠whatever. It should not stop on itâs own. Any thing done with setInterval should run indefinitely. It it is stopping, then there is something wrong with the computer, the browser, or codepen. I tell âadd greenâ one to run every 1000ms, then I wait 500ms and tell the âadd blueâ one to run every 1000ms also. There is no limit to how many times they run.
I donât know why yours is stopping, mine is still running after 10 minutes. It should keep running until I stop it or the aforementioned lizard man attack.
Oh ok hahahaâŚthatâs weird that itâs stoppingâŚI mean my computer has little freezing problems every now and then but idkâŚthe one at https://jsfiddle.net/MenelikMakonnen/4v2drbao/, is still running. Maybe because itâs a CSS animation, and maybe those are âlighterâ and run better, just thinking out loudâŚ
Donât know - mine is still going strong.
1 Like
Thankyou for all your help man. Mad thanks for when yâall coders take time to show me something. Was glad to see your username pop up on this post today! Cheers!
Thatâs why weâre here. Weâre just a little further up the ladder. In no time youâll be helping those below you.
1 Like