JavaScript学习笔记(二)防抖函数

function debounce(func, waite) {
  let timerId = null
  return function(...args) {
    if (timerId) {
      clearTimeout(timerId)
    }
    timerId = setTimeout(() => {
      func(...args)
    }, waite)
  }
}
let txtChange = debounce(event => {
  console.log(event.target.value)
}, 500)
document.getElementById('txt').onkeyup = event => {
  txtChange(event)
}

 

posted @ 2019-03-25 10:11  zhoulixue  阅读(108)  评论(0编辑  收藏  举报