Skip to content
📁 标签
JavaScript手写题

防抖函数

js
function throttle(fn, time = 500) {
    let lastTime = Date.now()
    let context = this
    return function (...args) {
        nowTime = Date.now()
        if (nowTime - lastTime > time) {
            fn.apply(context, args)
            // 更新时间
            lastTime = nowTime
        }
    }
}