首页 > 其他分享 >防抖就是在事件在同一时间内多次触发只执行最后一次 比如你设置1000毫秒定时,但是在500毫秒时又被重新调用了 但只执行一次

防抖就是在事件在同一时间内多次触发只执行最后一次 比如你设置1000毫秒定时,但是在500毫秒时又被重新调用了 但只执行一次

时间:2023-01-08 14:33:13浏览次数:37  
标签:防抖 timer 毫秒 500 input 同一时间

<!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

相关文章