Advertisement
bAngelov

Debounced Search Bar

Sep 30th, 2024 (edited)
99
0
114 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.        
  3.         function submit_search(e){
  4.             let timeout = null
  5.            
  6.             const form = document.querySelector(".searchbar")
  7.             const input = form.querySelector('input')
  8.             input.focus()
  9.             input.setSelectionRange(input.value.length, input.value.length)
  10.            
  11.             input.addEventListener('input',(e)=>{
  12.                 clearTimeout(timeout)
  13.                 timeout = setTimeout(()=>{
  14.                     form.submit()
  15.                 }, 1000)
  16.             })
  17.         }
  18.         submit_search()
  19. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement