Search code examples
javascriptjquerysetintervaljquery-slider

Setinterval for different events with jQuery


I have created my own slider. Each slide has different animations over different elements, so I have something like this for the buttons to change from one slide to another.

$('.content1').click(function(e){
    $('#image1').animate({'margin-left': '0px', 'margin-top': '0px'});
    $('#box1').animate({'margin-top': '0px'});
    hide2();  //hiddeing slide2
    hide3();  //hidding slide3
    e.preventDefault();
});

$('.content2').click(function(e){
    hide1(); //hidding slide1
    hide3();  //hidding slide2
    $('#box2').animate({'margin-top': '0px'});
    $('#image2').animate({'margin-top': '0px'});
    e.preventDefault();
}); 


$('.content3').click(function(e){
    hide1();
    hide2();
    $('#box3').animate({'margin-left': '0px'});
    $('#image3').stop().delay(1000).show().animate({'opacity': '1'});
    e.preventDefault();
});

I would like now to add an interval for the slider to move alone every X seconds.

I wonder if it is possible to make 3 different calls instead of just one function I am used to:

setInterval(function(){
     nameOfFunction();
    }, 6*1000);

Thanks.


Solution

  • Finally I solved it using setTimeout as @BradM recommended.

    Im sure there's a more simple way, but here is how I did it. Here's the code:

    Menu click events:

    $('.content1').click(function(e){
        slide1();
        e.preventDefault();
    });
    
    $('.content2').click(function(e){
        slide2();
        e.preventDefault();
    }); 
    
    $('.content3').click(function(e){
        slide3();
        e.preventDefault();
    });
    

    Slide effects

    function slide1(){
        next = 2; //setting which is gonna be the next slide
        $('#image1').animate({'margin-left': '0px', 'margin-top': '0px'});
        $('#box1').animate({'margin-top': '0px'});
    
        hide2();
        hide3();
    }
    
    function slide2(){
        next = 3; //setting which is gonna be the next slide
        hide1();
        hide3();
        $('#box2').animate({'margin-top': '0px'});
        $('#image2').animate({'margin-top': '0px'});
    }
    
    function slide3(){
        next = 1; //setting which is gonna be the next slide
        hide1();
        hide2();
        $('#box3').animate({'margin-left': '0px'});
        $('#image3').stop().delay(1000).show().animate({'opacity': '1'});
    }
    

    Hidding functions

        function hide1(){
        $('#image1').animate({'margin-left': '2000px'});
        $('#box1').animate({'margin-top': '-2000px'});
    }
    
    function hide2(){
        $('#box2').animate({'margin-top': '600px'});
        $('#image2').animate({'margin-top': '-2000px'});
    }
    
    function hide3(){
        $('#box3').animate({'margin-left': '-2000px'});
        $('#image3').animate({'opacity' : '0', 'display' : 'none'}, function(){
            $(this).hide();
        });
    }
    

    Onmouse over/out actions

    //when its over, we stop the movement
    var delay = 6000; //6 seconds
    $('#slider').hover(function(){
        clearTimeout(timer);
    
    }, function(){
    //starting again the animation (since the last slide)   
        timer = window.setTimeout(playSlide, delay);
    });
    

    Main playslide function

    var next = 2;  //next slide will be the 2nd one at the start
    function playSlide(){
        var name = "slide" + next;
        var method = eval(name);
    
        method();
    
        //calling recursively the function
        timer = window.setTimeout(playSlide, delay);
    }
    

    First call

      //only if there's an slider, we initate it
    if($('#slider').length){
        //first call the the slider
        timer = window.setTimeout(playSlide, delay);
    }