实现防抖函数debounce

学习 · 2021-11-30
function debounce(func, wait, immediate) {

var timeout, result;

var debounced = function () {
    var context = this;
    var args = arguments;

    if (timeout) clearTimeout(timeout);
    if (immediate) {
        // 如果已经执行过,不再执行
        var callNow = !timeout;
        timeout = setTimeout(function(){
            timeout = null;
        }, wait)
        if (callNow) result = func.apply(context, args)
    }
    else {
        timeout = setTimeout(function(){
            result = func.apply(context, args)
        }, wait);
    }
    return result;
};

debounced.cancel = function() {
    clearTimeout(timeout);
    timeout = null;
};

return debounced;

}

js 防抖 debounce
Theme Jasmine by Kent Liao | 桂ICP备15008025号-6