Mali
Mali

Reputation: 131

Using jQuery, how do I repeat a function in different intervals?

I need the function to repeat after 8 seconds the first time and then again after 30 seconds. I have used setTimeout but not sure if this is the way to go about it. Thank you.

Upvotes: 2

Views: 490

Answers (3)

lowerkey
lowerkey

Reputation: 8345

Use javascript:

var interval = setInterval(yourFunction, 8000);

Where 8000 is the interval after which your function is executed again, and interval is a handle to the interval, in case you want to stop it using clearInterval

Upvotes: 0

Cameron Martin
Cameron Martin

Reputation: 6010

setTimeout is perfect.

(function() {
    var func = function() {
        // Code here
    }
    setTimeout(func, 8000);
    setTimeout(func, 30000);
})();

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039458

You could use a conjunction of setTimeout and setInterval:

window.setTimeout(function() {
    // this will run 8 seconds later

    window.setInterval(function() {
        // do here whatever you want to do at 30 seconds intervals
    }, 30000);    
}, 8000);

Upvotes: 6

Related Questions