【中级-好题】周期执行某个函数n次
const repeatFunc = repeat(console.log, 4, 3000);
repeatFunc("helloworld");
// 每3秒打印一个helloworld,总计执行4次
1
2
3
4
2
3
4
参考答案:
const repeat = function(fn, times, interval) {
let count = 0;
return function returnFn(...args) {
if (count < times) {
setTimeout(() => {
fn(...args);
count++;
returnFn(...args);
}, interval);
}
};
};
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13