1.防抖js
export function debounce(func, delay) {
let timer = null
return function (...args) {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
export function throttle(fn, delay) {
let timer;
let t_start = Date.now();
return function () {
const context = this;
const args = arguments;
const t_curr = Date.now();
clearTimeout(timer);
if (t_curr - t_start >= delay) {
fn.apply(context, args);
t_start = t_curr;
// console.log("1")
} else {
timer = setTimeout(() => {
fn.apply(context, args);
}, delay);
}
};
}
2.引入
import {debounce} from '@/api/utils/debounce'
3.使用方法
myClick:debounce(function() {
//想要做的事
}, 1000),