Search code examples
javascriptjqueryjquery-pluginstimer

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


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.


Solution

  • 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)