console
var i = document.querySelector('input')
function debounce(fn,delay) {
let timer = null
return function() {
clearTimeout(timer)
// 如果绑定this,结果是input,不绑定的话是window。
// 而且debounce返回的也不能是箭头函数,否则它的this就是debounce的this,即window
timer = setTimeout(fn.bind(this),delay)
// timer = setTimeout(()=>fn(),delay)
}
}
function throttle(fn,delay) {
let timer = null
return function() {
if (timer) return
timer = setTimeout(()=>{
timer = null
fn.bind(this)(arguments)
}, delay)
}
}
function func() {
var p = document.createElement('p')
p.innerText = this
document.body.appendChild(p)
}
i.oninput = throttle(func,1000)
<input type="text">
<input type="text">