debounce------防抖------

62 阅读1分钟
// 使用方法  
var box = document.getElementById('box')

box.onclick = debounce(callBack, 500, 1, 2, 3, 4, 5, 6)
function callBack(...arg) {   
    console.log('事件对象及参数', arg)
}  
function debounce(fn, delay, ...arg) {    
    var timer = null    
    return (...e) => {      
        clearTimeout(timer)      
        timer = setTimeout(() => {        
            let ary = [...e, ...arg]        
            fn.apply(this, ary)      
        }, delay)    
    } 
}