js函数防抖

function dedounce(fn, duration) {
    let timeId;
    // 此处的this指向windows
    return function () {
        // 此处的this指向调用函数的对象
        const that = this;
        if (timeId) {
            clearTimeout(timeId);
        }
        // 这个地方的函数是事件函数
        const args = Array.prototype.slice.call(arguments, 0);

        timeId = setTimeout(function () {
            fn.apply(that, args)
        }, duration)
    }
}

input.addEventListener('keyup', dedounce(function (e) {
    span.innerHTML = this.value;
}, 1000));

posted on 2024-04-14 12:03  ccblblog  阅读(16)  评论(0)    收藏  举报

导航