fix throttle func
This commit is contained in:
@@ -48,7 +48,7 @@ export const sleep = (delay) =>
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const debounce = (func, delay = 200) => {
|
export const debounce = (func, delay = 200) => {
|
||||||
let timer;
|
let timer = null;
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
timer && clearTimeout(timer);
|
timer && clearTimeout(timer);
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
@@ -66,14 +66,22 @@ export const debounce = (func, delay = 200) => {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const throttle = (func, delay = 200) => {
|
export const throttle = (func, delay = 200) => {
|
||||||
let timer;
|
let timer = null;
|
||||||
|
let cache = null;
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
if (!timer) {
|
if (!timer) {
|
||||||
func(...args);
|
func(...args);
|
||||||
|
cache = null;
|
||||||
timer = setTimeout(() => {
|
timer = setTimeout(() => {
|
||||||
|
if (cache) {
|
||||||
|
func(...cache);
|
||||||
|
cache = null;
|
||||||
|
}
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
timer = null;
|
timer = null;
|
||||||
}, delay);
|
}, delay);
|
||||||
|
} else {
|
||||||
|
cache = args;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user