节流防抖
防抖的应用
function debounce(fn,delay) {
let timer
return function() {
clearTimeout(timer)
var args = arguments
timer = setTimeout(function() {
fn(args)
},delay)
}
}
function a(value) {
console.log('输出', value)
}
const input = document.getElementById('ipt')
const debounceInput = debounce(a,1000)
input.addEventListener('keyup',function(e) {
debounceInput(e.target.value)
})
function throttle(fn,delay){
let timer
return function() {
var args = arguments
if(!timer){
timer = setTimeout(function(){
fn(args)
clearTimeout(timer)
timer = null
},delay)
}
}
}
努力到无能为力,拼搏到感动自己。