首页 > 其他 > 详细

vue函数节流防抖

时间:2021-03-31 16:17:00      阅读:39      评论:0      收藏:0      [点我收藏+]
//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),
}

 

vue函数节流防抖

原文:https://www.cnblogs.com/minghan/p/14601055.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!