Debounce in JavaScript and free variable


#1

We know a debounce function could write like this:


function debounce(fn) {
  let holdOn = null;
  return () => {
    if(holdOn) {
      clearTimeout(holdOn)
      holdOn = null
    }
    
    holdOn = setTimeout(fn, 3000)
  }
}

let haha = () => {
  console.log('go go go')
}

let debounceHaha = debounce(haha)

debounceHaha()
debounceHaha()
debounceHaha()
debounceHaha()

// there will be the only one 'go go go'

Do we have to use a free variable(like the holdOn in the above snippet) to achieve a debounce function?