Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript Call function 10 times with 1 second between

How to call a function 10 times like

for(x=0; x<10; x++) callfunction();

but with 1 sec between each call?

like image 971
user3129452 Avatar asked Dec 05 '25 04:12

user3129452


1 Answers

function callNTimes(func, num, delay) {
    if (!num) return;
    func();
    setTimeout(function() { callNTimes(func, num - 1, delay); }, delay);
}
callNTimes(callfunction, 10, 1000);

EDIT: The function basically says: make a call of the passed function, then after a bit, do it again 9 more times.

like image 186
Amadan Avatar answered Dec 07 '25 19:12

Amadan