Skip to content

节流

85 字小于 1 分钟

js

2024-12-28

节流

当持续触发事件时,保证一定时间段内只调用一次事件处理函数。

let btn = document.querySelector('button')
    function ajax(param) {
        console.log(param);
    }
    btn.addEventListener('click',() => {
            throttleAjax('节流!')
    })
    function Throttle(cb,wait) {
        let flag = true
        let that = this
        return function(param){
            if(flag) {
                flag = false
                setTimeout(() => {
                    cb.call(that,param)
                    flag = true
                },wait)
            }
        }
    }
let throttleAjax = Throttle(ajax,1000)