123456789101112131415161718192021222324252627 |
- const input = document.getElementById('input')
- // let timer = null
- // input.addEventListener('keyup', function (e) {
- // if (timer) {
- // clearTimeout(timer)
- // }
- // timer = setTimeout(() => {
- // console.log(e.target.value)
- // timer = null
- // }, 500)
- // })
- // 防抖
- function debounce (fn ,delay = 500) {
- let timer = null
- return function () {
- if (timer) {
- clearTimeout(timer)
- }
- timer = setTimeout( () => {
- fn.apply(this, arguments)
- }, delay)
- }
- }
- input.addEventListener('keyup', debounce(function(){
- console.log(this.value)
- }, 1000))
|