Gadgetster
Gadgetster

Reputation: 483

Multiple functions in one setInterval()?

I have two setInterval each running a function every 20s. Is there a way to put both functions under the same setInterval call?

setInterval(function(){
 //function a
}, 20000);

setInterval(function(){
 //function b
}, 20000);

To something like:

setInterval( (functionA, functionB) , 20000);

Upvotes: 10

Views: 19017

Answers (2)

PeterKA
PeterKA

Reputation: 24638

If you define a third function that calls the two functions, you can just call that third function as follows:

function functionC() {
   functionA();
   functionB();
}

setInterval( functionC, 20000 );

Upvotes: 5

Arun P Johny
Arun P Johny

Reputation: 388316

Just call both the functions within another callback function like

setInterval(function () {
    functionA();
    functionB();
}, 20000);

Upvotes: 23

Related Questions