yuxuannnn_utils
Version:
47 lines (46 loc) • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.throttle = exports.debounce = exports.delay = void 0;
/**
* 延迟函数一般用于本地开发时模拟服务器延迟
*/
function delay(delayTime = 1000) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(1);
}, delayTime);
});
}
exports.delay = delay;
/**
* 防抖
* @param func 执行函数
* @param delay 延迟时间
* @returns
*/
const debounce = (func, delay = 3000) => {
let timer = void 0;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
};
};
exports.debounce = debounce;
/**
*
* @param func
* @param duration 两次函数执行的时间间隔
*/
const throttle = (func, duration) => {
let lastTime = 0;
return function (...args) {
const nowTime = Date.now();
if (nowTime - lastTime >= duration) {
func.apply(this, args);
lastTime = nowTime;
}
};
};
exports.throttle = throttle;