console
let debButton = document.querySelector("#debounce")
let debounce = function(fn,wait=500){
let timeOut;
return function(){
const args = arguments;
if(timeOut){
clearTimeout(timeOut)
}
console.log(arguments[0])
timeOut = setTimeout(()=>{
fn.apply(this,args)
},wait)
}
}
let debFn = function(val){
console.log(`防抖----${val}---`)
}
let deb = debounce(debFn,1000)
debButton.onclick = function(){
deb(1)
}
let thrButton = document.querySelector("#throttle");
let throttle = function(fn,wait=500){
let timeOut;
return function(){
if(!timeOut){
const args = arguments;
timeOut = setTimeout(()=>{
fn.apply(this,args)
timeOut = null;
},wait)
}
}
}
let thrFn = function(val){
console.log(`节流---${val}---`)
}
let thr = throttle(thrFn,1000)
thrButton.onclick = function(){
thr(2)
}
<button id="debounce">防抖按钮</button>
<button id="throttle">节流按钮</button>