节流和防抖

    科技2024-06-02  70

    节流

    const throttle = (fn, interval) => { let last = Date.now() return function () { const self = this // 保存this const args = arguments // 保存arguments const now = Date.now() if (now - last > interval) { last = now fn.apply(self, args) } } }

     

    防抖

    const debounce = (fn, interval) => { let timer = null return function () { const self = this const args = arguments if (timer) { clearTimeout(timer) } setTimeout(() => { fn.apply(self, args) }, interval) } }
    Processed: 0.013, SQL: 8