1. 程式人生 > 其它 >vue中的防抖與節流

vue中的防抖與節流

參考文件:

https://www.jb51.net/article/161713.htm 或者
// 防抖
const debounce = (func, wait, immediate) => {
            let timeOut;
            return function () {
                const context = this;
                const args = arguments;
                if (timeOut) {
                    clearTimeout(timeOut);
                }
                
if (immediate) { let callNow = !timeOut; timeOut = setTimeout(() => { timeOut = null; }, wait || 500) if (callNow) { func.apply(context, args); } }
else { timeOut = setTimeout(() => { func.apply(context, args); }, wait || 500); } } }
// 元件中呼叫
<el-input type='text' @input="test($event)">

// 引入防抖
import {Debounce} from './utils';


export default
{ methods:{ test(e){ Debounce(()=>{ console.log(e); },1000) } } }