防抖一段时间内,最后一次触发
function but(fn,i){
let n;
return function(){
clearTimeout(n)
n = setTimeout(() => {
fn.call(this,i)
},i);
}
}
function a(b){
console.log("防抖成功"+b)
}
var oinp = document.querySelector('input')
oinp.addEventListener('click',but1(a,3000))
节流一段时间内,最后一次触发
function but1(fn,i){
let n = true;
return function(){
if(!n) {return}
n =false
setTimeout(() => {
fn.call(this,i)
n = true
}, i);
}
}
function a(b){
console.log("节流成功"+b)
}
var oinp = document.querySelector('input')
oinp.addEventListener('click',but1(a,3000))