概念
不断触发事件,但是在指定的单位时间内只触发一次
代码实现
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="text" id="input">
<script>
var input = document.querySelector("#input")
var timer;
input.onkeyup = function () {
// 停止操作后才执行1次
// 如果连续操作,则之前的延迟执行都被清除了
clearTimeout(timer)
timer = setTimeout(function () {
console.log("开始执行搜索...")
}, 1000);
}
</script>
</body>
</html>