重复执行函数

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()
原文地址:https://www.cnblogs.com/zhenjianyu/p/13965009.html