<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)
}
}
方法没写对,一般都是 arguments 然后 apply 进去