文章发布于:2022年11月6日

不让函数或对象方法密集地执行,设置一个“冷却”时间。

function coolingFn(fn, time = 0, context) {
    let ready = true;
    return function cooling() {
        if (ready) {
            if (time) {
                ready = false;
                setTimeout(() => {
                    ready = true;
                }, time)
            }
            return fn.apply(context || this, arguments);
        }
    }
}//函数const f1 = coolingFn(fn, 1000);//对象方法const f2 = coolingFn(obj.method, 1000, obj);