Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple functions in one setInterval()?

Tags:

jquery

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);
like image 688
Gadgetster Avatar asked May 20 '14 02:05

Gadgetster


People also ask

Can two setInterval be used in same code?

Good question, but in JS you can't. To have multiple functions in the same program execute at the same time you need multi-threading and some deep timing and thread handling skills. JS is single threaded.

Why you should not use setInterval?

In case of time intensive synchronous operations, setTimeInterval may break the rhythm. Also, if any error occurs in setInterval code block, it will not stop execution but keeps on running faulty code. Not to mention they need a clearInterval function to stop it.

Does the setInterval () function work in JavaScript?

The setInterval() method in JavaScript is used to repeat a specified function at every given time-interval. It evaluates an expression or calls a function at given intervals. This method continues the calling of function until the window is closed or the clearInterval() method is called.

How does the setInterval () function work in?

The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .


1 Answers

Just call both the functions within another callback function like

setInterval(function () {
    functionA();
    functionB();
}, 20000);
like image 174
Arun P Johny Avatar answered Jan 29 '23 17:01

Arun P Johny