vue函数节流防抖

//js
/**
 * 函数节流
 * @param func
 * @param wait
 * @returns {function(...[*]=)}
 */
export const throttle = (func, wait = 1500) => {
        let timeout;
        return function() {
            let context = this;
            let args = arguments;
            if (!timeout) {
                timeout = setTimeout(() => {
                    timeout = null;
                    func.apply(context, args)
                }, wait)
            }
        }
    }
    /**
     * 函数防抖
     * @param func
     * @param wait
     * @returns {function(...[*]=)}
     */
export const debounce = (func, wait = 1500) => {
    let timeout;
    return function() {
        let context = this;
        let args = arguments;
        if (timeout) clearTimeout(timeout);
        timeout = setTimeout(() => {
            func.apply(context, args)
        }, wait);
    }
}
//vue
import { debounce, throttle } from "./tool";
methods: {
     tabbtn: debounce(function (index) {
     }, 1000),
}

 

posted @ 2021-03-31 13:32  茗涵  阅读(97)  评论(0编辑  收藏  举报