export const throttle = (fn, wait = 1500) => {
let inThrottle, lastFn, lastTime;
return function() {
const context = this,
args = arguments;
if (!inThrottle) {
fn.apply(context, args);
lastTime = Date.now();
inThrottle = true;
} else {
clearTimeout(lastFn);
lastFn = setTimeout(function() {
if (Date.now() - lastTime >= wait) {
fn.apply(context, args);
lastTime = Date.now();
}
}, Math.max(wait - (Date.now() - lastTime), 0));
}
};
};
export const debounce = (fn, delay = 1000) => {
let timer = null;
return function() {
let context = this;
let args = arguments;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function() {
fn.apply(context, args);
}, delay);
};
};
import {throttle,debounce} from "@/utils/potting";
btn1:debounce(()=>{
console.log(132)
},3000)
btn2:throttle(()=>{
console.log(321)
},1000)