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

ensnare picture ensnare · Jan 31, 2010 · Viewed 560.8k times · Source

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.

Answer

Doug Neiner picture Doug Neiner · Jan 31, 2010

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

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

To stop the loop you can use

clearInterval()