<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="input">
</body>
<script>
var input = document.querySelector('input')
function debounce(delay) {
let timer
//闭包
return function(value) {
clearTimeout(timer)
// 我们想清楚setTimeout 我们应该存储这个timer变量,timer 变量要一直保存在内存中
// 既然你不想打印之前已存储的结果,清楚之前的定时器
// 我们存储这个timer变量, 一直在内存, 内寻泄露
// 使用闭包,函数中return 函数
timer = setTimeout(function() {
console.log(value)
}, delay)
}
}
var debounceFun = debounce(1000)
input.addEventListener('keyup', function(e) {
debounceFun(e.target.value)
})
</script>
</html>