Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to call a function every 5 seconds in jQuery? [duplicate]

JQuery, how to call a function every 5 seconds.

I'm looking for a way to automate the changing of images in a slideshow.

I'd rather not install any other 3rd party plugins if possible.

like image 374
ensnare Avatar asked Jan 31 '10 07:01

ensnare


People also ask

How do you run a function every 5 seconds?

To call a JavaScript function every 5 seconds continuously, we call setInterval with the function that we want to run and the interval between runs. const interval = setInterval(() => { // ... }, 5000); clearInterval(interval); to call setInterval with the callback we want to run and 5000 millisecond period.

How do I call a function after 2 seconds in jQuery?

To call a jQuery function after a certain delay, use the siteTimeout() method. Here, jQuery fadeOut() function is called after some seconds.

How can we delay calling a function after 5 seconds?

Answer: To delay a function call, use setTimeout() function.


2 Answers

You don't need jquery for this, in plain javascript, the following will work!

var intervalId = window.setInterval(function(){   /// call your function here }, 5000); 

To stop the loop you can use

clearInterval(intervalId)  
like image 188
Doug Neiner Avatar answered Oct 03 '22 13:10

Doug Neiner


you could register an interval on the page using setInterval, ie:

setInterval(function(){      //code goes here that will be run every 5 seconds.     }, 5000); 
like image 32
John Boker Avatar answered Oct 03 '22 13:10

John Boker