Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping/setinterval through multiple functions?

issue is, I have 4 functions i would like to sequentially run every 5 seconds or so, and I'm not sure how to go about it

I know the code is inefficient as it is. (right now i have it set up this way so the functions can also be triggered by clicked 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 greatly appreciated :].

like image 386
Dim Panda Avatar asked Dec 06 '22 19:12

Dim Panda


1 Answers

Just make a function that calls the other functions:

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

If you want to call them one at a time in a rotating order, put them in an array and keep an array index:

var intervalFunctions = [ tScroll1, tScroll2, tScroll3, tScroll4 ];
var intervalIndex = 0;
window.setInterval(function(){
  intervalFunctions[intervalIndex++ % intervalFunctions.length]();
}, 5000);
like image 167
Guffa Avatar answered Dec 10 '22 11:12

Guffa