// 节流 export function throttle({ delay = 300, callback = () => { } }) { return function (...args) { const { isRunning } = throttle if (isRunning) return throttle.isRunning = true setTimeout(() => { callback(...args) throttle.isRunning = null }, delay) } } // 防抖 export function debounce({ delay = 300, callback = () => { } }) { return function (...args) { const { lastid } = debounce lastid && clearTimeout(lastid) debounce.lastid = setTimeout(() => { callback(...args) // 释放内存 debounce.lastid = null }, delay) } }
使用方法:
debounce({ delay: 300, callback: () => { // TODO 防抖后回调... } })()标签:...,防抖,节流,debounce,delay,callback,lastid From: https://www.cnblogs.com/cdyun/p/17163479.html