|
|
@@ -172,28 +172,16 @@ export function jsonp2(url, params, callbackName = 'jsonp_callback') {
|
|
|
|
|
|
|
|
|
export function throttleAdvanced(fn, delay) {
|
|
|
- let timer = null;
|
|
|
- let lastExecTime = 0;
|
|
|
-
|
|
|
- return function(...args) {
|
|
|
- const context = this;
|
|
|
- const now = Date.now();
|
|
|
- const remaining = delay - (now - lastExecTime); // 计算剩余时间
|
|
|
-
|
|
|
- // 清除计划中的下一次执行(如果有)
|
|
|
- clearTimeout(timer);
|
|
|
-
|
|
|
- if (remaining <= 0) {
|
|
|
- // 剩余时间已到,立即执行
|
|
|
- fn.apply(context, args);
|
|
|
+ let lastExecTime = 0; // 记录上次成功执行的时间戳
|
|
|
+ return function(...args) { // 使用剩余参数接收所有传入参数
|
|
|
+ const now = Date.now(); // 获取当前时间戳
|
|
|
+ // 如果当前时间减去上次执行时间大于等于设定的延迟,则执行函数
|
|
|
+ if (now - lastExecTime >= delay) {
|
|
|
+ // 使用 apply 确保函数内部的 this 指向正确,并传递参数
|
|
|
+ fn.apply(this, args);
|
|
|
+ // 更新最后一次执行的时间戳为当前时间
|
|
|
lastExecTime = now;
|
|
|
- } else {
|
|
|
- // 否则,设置一个定时器,确保在剩余时间结束后还会执行一次
|
|
|
- timer = setTimeout(() => {
|
|
|
- fn.apply(context, args);
|
|
|
- lastExecTime = Date.now();
|
|
|
- timer = null;
|
|
|
- }, remaining);
|
|
|
}
|
|
|
+ // 如果时间间隔未到,则什么都不做,忽略此次调用
|
|
|
};
|
|
|
}
|