节流和防抖

🌺 摘要
节流和防抖

节流和防抖

节流

function debounce(fn, wait) {
     let timer = null
        return function () {
           if (timer) clearTimeout(timer)
                timer = setTimeout(() => {
                    fn.call(this, arguments)
                }, wait)
            }
        }

防抖

function throttle(fn, wait) {
            let timer = null
            return function () {
                if (!timer) {
                    timer = setTimeout(() => {
                        event()
                        timer = null
                    }, wait)

                }
            }
        }