add throttle func

This commit is contained in:
Gabe Yuan
2023-09-08 10:32:44 +08:00
parent f9a3ec012f
commit 35f01478b1

View File

@@ -53,10 +53,31 @@ export const debounce = (func, delay = 200) => {
timer && clearTimeout(timer); timer && clearTimeout(timer);
timer = setTimeout(() => { timer = setTimeout(() => {
func(...args); func(...args);
clearTimeout(timer);
timer = null;
}, delay); }, delay);
}; };
}; };
/**
* 节流函数
* @param {*} func
* @param {*} delay
* @returns
*/
export const throttle = (func, delay = 200) => {
let timer;
return (...args) => {
if (!timer) {
func(...args);
timer = setTimeout(() => {
clearTimeout(timer);
timer = null;
}, delay);
}
};
};
/** /**
* 判断字符串全是某个字符 * 判断字符串全是某个字符
* @param {*} s * @param {*} s