0

I tried to make a send request after I stop writing in the search box

i don't understand this part of the code

 return function(...e){ // i don't understand this line and from where we get ...e
        clearTimeout(timeout)
        timeout=setTimeout(()=>fn.apply(this,e),time)
    }

this debounce function work correctly but I don't understand why we return a function(...e) in it

function debounce(fn,time){
    let timeout
    return function(...e){ // i don't understand this line and from where we get ...e
        clearTimeout(timeout)
        timeout=setTimeout(()=>fn.apply(this,e),time)
    }
}
searchBox.addEventListener('input',debounce((e)=>{
    searchTerm=e.target.value;
   loadAllVideo(sortBy,searchTerm)
   },500))
   
  • 1
    Do you not understand why a function is returned (and how closures work) or do you not understand what the `...e` syntax means? – Bergi Feb 12 '21 at 20:13
  • I know the meaning of `...e` I don't understand why a function is returned – Ameen Reda Feb 13 '21 at 00:01
  • Because `addEventListener` expects one, and all calls to that particular function should share the `fn`, `time` and `timeout` variables – Bergi Feb 13 '21 at 09:23

0 Answers0