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