防抖和节流
function debounce (fn, t) {
var time = null
return function () {
var args = arguments
var self = this
clearTimeout(time)
time = setTimeout(function () {
fn.apply(self, args)
}, t)
}
}
function throttle (fn, t) {
var time = null
return function () {
var self = this
var args = arguments
if (time ) {
return
}
time = setTimeout(function () {
fn.apply(self, args)
time = null
}, t)
}
}
<input type="text" name="" id="inp">
var inp = document.getElementById('inp')
inp.oninput = debounce((e) => {
console.log(e.target.value)
}, 1000)
inp.oninput = throttle((e) => {
console.log(e.target.value)
}, 1000)