Looping / setinterval through multiple functions?

the problem is that I have 4 functions that I would like to run sequentially every 5 seconds or so, and I'm not sure how to do this

I know the code is inefficient as it is. (right now I have it configured so that functions can also be launched using the pressed buttons)

function tScroll1(){ var $testimonialScrollPos = parseInt($tCont.css('top'), 10); if($testimonialScrollPos == -915){ $tCont.css('top','305px'); $tCont.animate({top:'0px'},700); } else{ $tCont.animate({top:'0px'},700); } }; function tScroll2(){ $tCont.animate({top:'-305px'},700); }; function tScroll3(){ $tCont.animate({top:'-610px'},700); }; function tScroll4(){ $tCont.animate({top:'-915px'},700); }; $tBtns.eq(0).click(function(){tScroll1()}); $tBtns.eq(1).click(function(){tScroll2()}); $tBtns.eq(2).click(function(){tScroll3()}); $tBtns.eq(3).click(function(){tScroll4()}); 

Any help is appreciated:].

+4
source share
1 answer

Just create a function that calls other functions:

 window.setInterval(function(){ tScroll1(); tScroll2(); tScroll3(); tScroll4(); }, 5000); 

If you want to call them one by one in a rotating order, put them in an array and save the index of the array:

 var intervalFunctions = [ tScroll1, tScroll2, tScroll3, tScroll4 ]; var intervalIndex = 0; window.setInterval(function(){ intervalFunctions[intervalIndex++ % intervalFunctions.length](); }, 5000); 
+15
source

All Articles