Program a command every 5 seconds

Possible duplicate:
Do something every 5 seconds and code to stop it. (Jquery)

I have a gallery that I want to automatically launch without using me.

How can I run a script that calls it 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 that the gallery looks like it automatically scrolls.

$.galleryUtility.slideLeft( ) 
+3
source share
2 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 immediately after the gallery is initialized.

 <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 the self-timer stop, you need to clear the interval, and for this you need a reference to the return 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 start the auto slide, you call start_autoslide(); , and when you need to stop it, you call stop_autoslide();

+5
source
 var id = setInterval(function () { $.galleryUtility.slideLeft(); }, 5000); 

Every 5 seconds it will be called. If you need to stop it at some point or condition, the following is called:

 clearInterval(id); //You must pass in the id that was returned after calling setInterval() 

Clarification:

When you call setInterval() , it actually returns the integer value of the identifier. You save this and pass it to clearInterval(**id goes here**) to stop further actions of this code.

+1
source

All Articles