function repeat(fn,n,interval){ return (...rest) => { let cnt = 0 let timer = null timer = setInterval(() => { fn.apply(fn,rest) cnt++ if(cnt >= n){ clearInterval(timer) timer = null } },interval) } } function log(){ console.log(this) } const repeatFunc = repeat(log, 4, 1000) repeatFunc()