Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program command to be called every 5 seconds [duplicate]

Possible Duplicate:
Do something every 5 seconds and the code to stop it. (JQuery)

I have a gallery that i want to automatically run without me using it.

How can i run a script that calls this every 5 seconds or so on this http://www.meadmiracle.com/SlidingGalleryDemo1.htm

I need this to be called every 5 seconds or so bsaically so the gallery looks like it automatically scrolls.

$.galleryUtility.slideLeft( ) 
like image 575
soniccool Avatar asked Aug 19 '26 21:08

soniccool


1 Answers

You need to use the setInterval method

<script type="text/javascript">
    $(window).load(function(){
        setInterval( function(){
            $.galleryUtility.slideLeft() ;
        }, 5000);
    });
</script>

Update

In your specific case, you should add it right after you initialize the gallery.

<script language="javascript" type="text/javascript">
    $(function() {
        $('div.gallery img').slidingGallery(); // add it right after this line of you existing code
        setInterval( function(){
            $.galleryUtility.slideLeft() ;
        }, 5000);
    });
</script>

Update 2

To start stop the autoslide you will need to clear the interval, and to do so you need a reference to the returned value from the setInterval call.

<script type="text/javascript">
    var autoSlideInterval;

    function start_autoslide(){
        autoSlideInterval = setInterval( function(){
            $.galleryUtility.slideLeft() ;
        }, 5000);
    }

    function stop_autoslide(){
        clearInterval( autoSlideInterval );
    }

    $(function() {
        $('div.gallery img').slidingGallery(); 
        start_autoslide();
    });

</script>

now when you want to star the auto-slide you call start_autoslide(); and when you need to stop it you call stop_autoslide();

like image 158
Gabriele Petrioli Avatar answered Aug 21 '26 10:08

Gabriele Petrioli