<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<input type="text" id="debounce">
<script>
/* document.getElementById('debounce').oninput = function(){
console.log('执行了模糊查询');
} */
var processor = {
timeoutId: null,
performProcessing: function() {
console.log('发送模糊查询请求');
},
process:function(){
clearTimeout(this.timeoutId)
this.timeoutId = setTimeout(()=>{
this.performProcessing()
},300)
}
/*这个是错误的this指向出现问题 process:()=>{
console.log(this);
clearTimeout(this.timeoutId)
this.timeoutId = setTimeout(()=>{
this.performProcessing()
},300)
} */
}
document.getElementById('debounce').oninput = function(){
processor.process()
}
</script>
</body>
</html>