fe-notes icon indicating copy to clipboard operation
fe-notes copied to clipboard

立即执行防抖函数

Open Inchill opened this issue 5 years ago • 0 comments

function debounce(func, wait) {
  let timer = null
  let flag = true
  return function () {
    clearTimeout(timer)
    if (flag) {
      func.apply(this, arguments)
      flag = false
    }
    timer = setTimeout(() => { flag = true }, wait)
  }
}

在 vue 里使用:

methods: {
  toPurchaseNow: debounce(async function () {
    // do something
  }, 1000)
}

Inchill avatar Dec 17 '20 07:12 Inchill