/**
* 防抖 只执行一次 单位时间内重新触发不执行
*/
function debounce(fn,time){
let timer = null
return function() {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(fn, time)
}
}
/**
* 节流 单位时间内执行一次
*/
function throttle(fn, time) {
let timer = null
return function() {
if (timer) return
timer = setTimeout(function() {
fn()
clearTimeout(timer)
timer = null
}, time)
}
}
function test() {
let input = document.createElement('input')
input.addEventListener('input', debounce(fn, 1500))
document.body.appendChild(input)
}
function fn() {
console.log('aaa')
}
test()
来源:CSDN
作者:W ~
链接:https://blog.csdn.net/WANG_AFei/article/details/104226163