mirror of
https://github.com/rubickCenter/rubick
synced 2025-12-26 12:42:34 +08:00
✨ 主界面开发&插件运行容器开发&菜单开发
This commit is contained in:
40
src/common/utils/throttle.ts
Normal file
40
src/common/utils/throttle.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export default function throttle(func, wait, options?: any): () => void {
|
||||
let context, args, result;
|
||||
let timeout = null;
|
||||
let previous = 0;
|
||||
if (!options) options = {};
|
||||
const later = function () {
|
||||
previous = options.leading === false ? 0 : Date.now();
|
||||
timeout = null;
|
||||
result = func.apply(context, args);
|
||||
if (!timeout) context = args = null;
|
||||
};
|
||||
return () => {
|
||||
const now = Date.now();
|
||||
if (!previous && options.leading === false) previous = now;
|
||||
// 计算剩余时间
|
||||
const remaining = wait - (now - previous);
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
context = this;
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
args = arguments;
|
||||
if (remaining <= 0 || remaining > wait) {
|
||||
if (timeout) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
}
|
||||
previous = now;
|
||||
result = func.apply(context, args);
|
||||
if (!timeout) context = args = null;
|
||||
} else if (!timeout && options.trailing !== false) {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
timeout = setTimeout(later, remaining);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user