Question

How do I call a given function every X seconds?

In this case, I made a function that scrolls some images. I want the image to change based on a given interval of time, for example, every 5 seconds, but I really have no idea.

Was it helpful?

Solution

No need for jQuery here, plain JavaScript using setInterval() will do:

function myFunctionName() {
  //change image here
}
setInterval(myFunctionName, 5000);

Or the anonymous version:

setInterval(function () {
  //change image here
}, 5000);

OTHER TIPS

Try

setInterval(function, Xseconds);

If you want jQuery to do it for you, there is a plugin, jquery.cycle.all. This will make creating the transition very easy. If you're using jQuery already, then it might be a good fit. Otherwise, the setInterval is easy to work with that other posters already mentioned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top