Im running a code where I must create a function and call it for 3 times with 2 seconds delay between each.I am having trouble calling it more than once. how can i solve this?I use javascript. i create myfunc() to log hello in console every 2 seconds for 3 times.
const myFunc = () => console.log("hello");
const runFunc = async (func, times, delay) => {
for (let i = 0; i < times; i++) {
func();
if (i + 1 < times) await new Promise(res => setTimeout(res, delay));
}
}
runFunc(myFunc, 3, 2 * 1000);
or this
const myFunc = () => console.log("hello");
const runFunc = async (func, times, delay) => {
if (times >= 1) func();
times--;
if (times >= 1) {
const interval = setInterval(() => {
times--;
if (times <= 0) clearInterval(interval);
func();
}, delay);
}
}
runFunc(myFunc, 3, 2 * 1000);