debounce

by Alex Liu

HTML

<button id="button">
click me
</button>

JavaScript

function debounce(func, timer) {
  let start, now, t
  return function() {
    if (t) {
      clearTimeout(t)
      t = null
    }
    t = setTimeout(function() {
      func.apply(this, arguments)
    }, timer)

  }

}

const button = document.getElementById("button")

function alert2(name) {
  console.log(`haha, ${name}`)
}

const alert2WithThrootle = throttle(alert2, 2000)
button.onclick = alert2WithThrootle