节流与防抖

节流与防抖

徐徐
前端
发布于2023-09-28 00:00:17
🌺前言
前端手动实现节流防抖

节流

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

防抖

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

                }
            }
        }

目录

文章最后更新于 2024-08-30 16:39:56
留言
暂无数据

~~空空如也