前端简单防抖功能
直接上代碼:
html:
<button @click="debounce(getVideo,'',500)">播放</button>data里聲明一個字段為null:
timeOut: null,methods寫入方法:
// 防抖函數(要調用的方法,傳入的參數,時間)debounce(fn, data, delay) {// 判斷方法是否已經被調用過if (this.timeOut) {// 如果被調用過就清除定時器clearTimeout(this.timeOut);}// 調用方法給timeOut賦值一個延時器this.timeOut = setTimeout(()=> {fn(data);}, delay);},完整代碼:
總結