<input id="content" type="text">
<script>
let input = document.getElementById('content');
let fn = debounce(searchResult, 500);
input.addEventListener('input', fn, false);
function searchResult(event) {
let target = event.target; // Cannot read property 'target' of undefined
}
function debounce(fn, interval) {
let timer = null;
return function() {
console.log(this); // <input id="content" type="text">
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function(){
fn();
}, interval)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…