CoreyRS
CoreyRS

Reputation: 2267

Get named function to repeat itself continuously in jQuery

I'm trying to get a function to repeat itself once it starts, but I must be off today because I can't figure it out. Here's my code:

function runNext() {
    galleryNext().delay(1500).runNext();
}

Thanks in advance.

Upvotes: 5

Views: 19255

Answers (2)

jessegavin
jessegavin

Reputation: 75650

If you want to call a function on a regular interval you should use setInterval().

var myFunction = function() {};
setInterval(myFunction, 1000); // call every 1000 milliseconds

If you ever need to stop the function from being called forever, setInterval() returns an id that you can use to stop the timer as well. Here's an example.

var myFunction = function() {};
var timer = setInterval(myFunction, 1000);
clearTimeout(timer);

Upvotes: 18

NedStarkOfWinterfell
NedStarkOfWinterfell

Reputation: 5153

You can't attach a user defied function like a DOM method. jQuery chaining applies to DOM manipulation and animations, because each process returns the DOM node in question. Plus delay just delays an animation, what you want here is to run a function after some time constantly, so use setTimeout:

(function foo() {
  setTimeout(function() { foo() },1000);
  })();

Upvotes: 2

Related Questions