美文网首页
debounce 和 throttle

debounce 和 throttle

作者: bestCindy | 来源:发表于2020-10-28 21:35 被阅读0次
const debounce = (fn, time) => {
    let timeout = null;
    return function() {
        clearTimeout(timeout);
        timeout = setTimeout(() => {
            fn.apply(this, arguments);
        }, time);
    }
}
const throttle = (fn, time) => {
    let flag = true;
    return function() {
        if (!flag) return;
        flag = false;
        setTimeout(() => {
            fn.apply(this, arguments);
            flag = true;
        },time);
    }
}

相关文章

网友评论

      本文标题:debounce 和 throttle

      本文链接:https://www.haomeiwen.com/subject/ixthvktx.html