//防抖
const inputHandler = function (e) {
console.log(e.target);
console.log(e.target.value);
}
ip.addEventListener('input', mydebounce(inputHandler, 1000))
function mydebounce(fn, delay) {
let timer = null
return function (...args) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(null, args)
timer = null
}, delay)
}
}
// 节流
const clickHandler=function(e){
console.log('hello');
}
btn.addEventListener('click',mythrottle(clickHandler,1000))
function mythrottle(fn,delay){
let timer=null
return function(...args){
if(!timer){
fn.apply(null,args)
timer=setTimeout(()=>{
timer=null
},delay)
}
}
}