vue 防抖节流

// 防抖 一定时间内事件只执行一次
export function _debounce(fn, delay) {
    var delay = delay || 200;
    var timer;
    return function () {
        var th = this;
        var args = arguments;
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(function () {
            timer = null;
            fn.apply(th, args);
        }, delay);
    };
}

// 节流 重复点击多次只执行一次
export function _throttle(fn, interval) {
    var last;
    var timer;
    var interval = interval || 200;
    return function () {
        var th = this;
        var args = arguments;
        var now = +new Date();
        if (last && now - last < interval) {
            clearTimeout(timer);
            timer = setTimeout(function () {
                last = now;
                fn.apply(th, args);
            }, interval);
        } else {
            last = now;
            fn.apply(th, args);
        }
    }
}·




// 使用
import {_throttle } from '@/utils/index'
//刷新数据
事件名: _throttle(function (e) {
	// 事件要执行的代码
}, 2000),
    
import { _debounce } from '@/utils/index'
事件名: _debounce(function (e) {}, 500),

你可能感兴趣的:(scss,typescript,vue.js)