I'm making a clock UI that shows analog, digital, and date at the same time. I made a function for each case that requires setInterval(). I wanted to know if I could write them at once in case there were many.
function getAnalog(){
return 'something';
}
function getDigital(){
return 'something';
}
function getDate(){
return 'something';
}
setInterval(getAnalog,1000);setInterval(getDigital,1000);setInterval(getDate,1000);
getAnalog();getDigital();getDate();
CodePudding user response:
You can create another function that calls all of the other functions.
function getAnalog(){
return 'something';
}
function getDigital(){
return 'something';
}
function getDate(){
return 'something';
}
const fn = () => {
getAnalog();
getDigital();
getDate();
};
setInterval(fn, 1000);
fn();
CodePudding user response:
// create an array with each function
const functions = [
function getAnalog(){
return 'something';
},
function getDigital(){
return 'something';
},
function getDate(){
return 'something';
},
]
functions.forEach(fn => {
setInterval(fn, 1000) // create each interval in the loop
fn() // run each function in the loop
})