Debounce

by ChrisStanyon

HTML

<input id="myInput">

JavaScript

document.getElementById('myInput').onkeyup = debounce(function() {
	console.log(this.value)
})

function debounce(fn, ms = 300) {
  let timer;
  return function(...args) {
    clearTimeout(timer)
    timer = setTimeout(fn.bind(this, ...args), ms || 0)
  }
}