1. 程式人生 > >vue 中使用防抖和節流,防止重複點選或重複上拉載入

vue 中使用防抖和節流,防止重複點選或重複上拉載入

/**
 * 函式防抖 (只執行最後一次點選)
 * @param fn
 * @param delay
 * @returns {Function}
 * @constructor
 */
export const Debounce = (fn, t) => {
    let delay = t || 500;
    let timer;
    console.log(fn)
    console.log(typeof fn)
    return function () {
        let args = arguments;
        if(timer){
            clearTimeout
(timer); } timer = setTimeout(() => { timer = null; fn.apply(this, args); }, delay); } }; /** * 函式節流 * @param fn * @param interval * @returns {Function} * @constructor */ export const Throttle = (fn, t) => { let last; let timer; let
interval = t || 500; return function () { let args = arguments; let now = +new Date(); if (last && now - last < interval) { clearTimeout(timer); timer = setTimeout(() => { last = now; fn.apply(this, args)
; }, interval); } else { last = now; fn.apply(this, args); } } };

用法

...
methods:{
	getAliyunData:Throttle(function(){
	...
	 },1000),
}
...