Debounce (or delay) a function

PHOTO EMBED

Tue Aug 09 2022 19:29:26 GMT+0000 (Coordinated Universal Time)

Saved by @johnnye562 #javascript

const debounce = (func, timeout = 2500) => {
  if (typeof func !== "function") {
    console.error(`func must be a valid function, ${typeof func} provided`)
    return
  }
  let timer
  return (...args) => {
    clearTimeout(timer)
    timer = setTimeout(() => { 
      func.apply(this, args) 
    }, timeout)
  }  
}
content_copyCOPY

https://justacoding.blog/9-useful-javascript-utility-functions/