<!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>防抖</title>
</head>
<body>
<input type="text">
</body>
<script>
// 防抖就是在事件在同一时间内多次触发只执行最后一次
let timer = null
let input = document.querySelector('input')
input.addEventListener('input', function () {
if (timer != null) {
clearTimeout(timer)
}
timer = setTimeout(() => {
console.log("防抖")
}, 500)
})
</script>
</html>
标签:防抖,timer,毫秒,500,input,同一时间
From: https://www.cnblogs.com/orangeczs/p/17034646.html