uview-plus组件库全面升级更新,订单结算判断支付方式是否可用代码调整,公众号关注二维码修改
This commit is contained in:
89
uni_modules/lime-shared/throttle/index.ts
Normal file
89
uni_modules/lime-shared/throttle/index.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
// @ts-nocheck
|
||||
/**
|
||||
* 节流函数,用于限制函数的调用频率
|
||||
* @param fn 要进行节流的函数
|
||||
* @param interval 两次调用之间的最小间隔时间
|
||||
* @returns 节流后的函数
|
||||
*/
|
||||
export type ThrottleOptions = {
|
||||
/**
|
||||
* 是否在节流开始时立即执行
|
||||
* @default true
|
||||
*/
|
||||
leading ?: boolean;
|
||||
|
||||
/**
|
||||
* 是否在节流结束后执行最后一次调用
|
||||
* @default true
|
||||
*/
|
||||
trailing ?: boolean;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 节流函数,限制函数在一定时间内只能执行一次
|
||||
* @param func 需要节流的函数
|
||||
* @param wait 节流间隔时间(毫秒)
|
||||
* @param options 配置选项
|
||||
* @param options.leading 是否在节流开始时立即执行(默认 true)
|
||||
* @param options.trailing 是否在节流结束时执行最后一次调用(默认 true)
|
||||
* @returns 返回节流后的函数
|
||||
*/
|
||||
// #ifndef APP-ANDROID
|
||||
export function throttle<T extends (...args : any[]) => any>(
|
||||
func : T,
|
||||
wait : number,
|
||||
options : ThrottleOptions = {}
|
||||
) : (...args : Parameters<T>) => void {
|
||||
let lastCallTime = 0;
|
||||
let timerId : ReturnType<typeof setTimeout> | null = null;
|
||||
const { leading = true, trailing = true } = options;
|
||||
return function (...args : Parameters<T>) {
|
||||
const now = Date.now();
|
||||
const timeSinceLastCall = now - lastCallTime;
|
||||
// 1. 如果 leading=true 且距离上次调用超过 wait,立即执行
|
||||
if (leading && timeSinceLastCall >= wait) {
|
||||
lastCallTime = now;
|
||||
func.apply(this, args);
|
||||
}
|
||||
// 2. 如果 trailing=true,设置定时器在 wait 时间后执行最后一次调用
|
||||
else if (trailing && !timerId) {
|
||||
timerId = setTimeout(() => {
|
||||
lastCallTime = Date.now();
|
||||
func.apply(this, args);
|
||||
timerId = null;
|
||||
}, wait - timeSinceLastCall);
|
||||
}
|
||||
};
|
||||
}
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-ANDROID
|
||||
export function throttle<T extends any|null>(
|
||||
func: (args : T) => void,
|
||||
wait : number,
|
||||
options : ThrottleOptions = {}
|
||||
) : (args : T) => void {
|
||||
let lastCallTime = 0;
|
||||
let timerId = -1;
|
||||
const { leading = true, trailing = true } = options;
|
||||
return function (args : T) {
|
||||
const now = Date.now();
|
||||
const timeSinceLastCall = now - lastCallTime;
|
||||
// 1. 如果 leading=true 且距离上次调用超过 wait,立即执行
|
||||
if (leading && timeSinceLastCall >= wait) {
|
||||
lastCallTime = now;
|
||||
func(args);
|
||||
}
|
||||
// 2. 如果 trailing=true,设置定时器在 wait 时间后执行最后一次调用
|
||||
else if (trailing && timerId > -1) {
|
||||
timerId = setTimeout(() => {
|
||||
lastCallTime = Date.now();
|
||||
func(args);
|
||||
timerId = -1;
|
||||
}, wait - timeSinceLastCall);
|
||||
}
|
||||
};
|
||||
}
|
||||
// #endif
|
||||
112
uni_modules/lime-shared/throttle/index_old.ts
Normal file
112
uni_modules/lime-shared/throttle/index_old.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
// @ts-nocheck
|
||||
/**
|
||||
* 节流函数,用于限制函数的调用频率
|
||||
* @param fn 要进行节流的函数
|
||||
* @param delay 两次调用之间的最小间隔时间
|
||||
* @returns 节流后的函数
|
||||
*/
|
||||
|
||||
// #ifndef UNI-APP-X && APP
|
||||
// export function throttle(fn: (...args: any[]) => void, delay: number, options) {
|
||||
// let flag = true; // 标记是否可以执行函数
|
||||
|
||||
// return (...args: any[]) => {
|
||||
// if (flag) {
|
||||
// flag = false; // 设置为不可执行状态
|
||||
// fn(...args); // 执行传入的函数
|
||||
|
||||
// setTimeout(() => {
|
||||
// flag = true; // 经过指定时间后,设置为可执行状态
|
||||
// }, delay);
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
|
||||
export function throttle<T extends (...args: any[]) => void>(
|
||||
fn: T,
|
||||
delay: number,
|
||||
{ leading = true, trailing = true }: { leading?: boolean; trailing?: boolean } = {}
|
||||
): (...args: Parameters<T>) => void {
|
||||
let isCoolingDown = false;
|
||||
let lastArgs: Parameters<T> | null = null;
|
||||
let timerId: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const executeTrailing = () => {
|
||||
if (trailing && lastArgs) {
|
||||
fn(...lastArgs);
|
||||
lastArgs = null;
|
||||
}
|
||||
isCoolingDown = false;
|
||||
timerId = null;
|
||||
};
|
||||
|
||||
return function (...args: Parameters<T>) {
|
||||
// 1. 如果不在冷却期,且 leading=true,立即执行
|
||||
if (!isCoolingDown && leading) {
|
||||
fn(...args);
|
||||
isCoolingDown = true;
|
||||
timerId = setTimeout(executeTrailing, delay);
|
||||
}
|
||||
// 2. 如果在冷却期,记录最后一次调用的参数(用于 trailing)
|
||||
else if (trailing) {
|
||||
lastArgs = args;
|
||||
}
|
||||
};
|
||||
}
|
||||
// #endif
|
||||
|
||||
|
||||
// #ifdef UNI-APP-X && APP
|
||||
// type Rfun = (...args: any[]) => void
|
||||
// type Rfun = (...args: any[]) => void
|
||||
|
||||
export function throttle<T extends any|null>(
|
||||
fn: (args : T) => void,
|
||||
delay: number):(args : T) => void {
|
||||
let flag = true; // 标记是否可以执行函数
|
||||
|
||||
return (args : T) =>{
|
||||
if(flag){
|
||||
flag = false;
|
||||
fn(args);
|
||||
|
||||
setTimeout(()=>{
|
||||
flag = true;
|
||||
}, delay)
|
||||
}
|
||||
}
|
||||
// return (...args: any[]) => {
|
||||
// // if (flag) {
|
||||
// // flag = false; // 设置为不可执行状态
|
||||
// // fn(...args); // 执行传入的函数
|
||||
|
||||
// // setTimeout(() => {
|
||||
// // flag = true; // 经过指定时间后,设置为可执行状态
|
||||
// // }, delay);
|
||||
// // }
|
||||
// };
|
||||
}
|
||||
|
||||
// #endif
|
||||
|
||||
// // 示例
|
||||
// // 定义一个被节流的函数
|
||||
// function handleScroll() {
|
||||
// console.log("Scroll event handled!");
|
||||
// }
|
||||
|
||||
// // 使用节流函数对 handleScroll 进行节流,间隔时间为 500 毫秒
|
||||
// const throttledScroll = throttle(handleScroll, 500);
|
||||
|
||||
// // 模拟多次调用 handleScroll
|
||||
// throttledScroll(); // 输出 "Scroll event handled!"
|
||||
// throttledScroll(); // 不会输出
|
||||
// throttledScroll(); // 不会输出
|
||||
|
||||
// // 经过 500 毫秒后,再次调用 handleScroll
|
||||
// setTimeout(() => {
|
||||
// throttledScroll(); // 输出 "Scroll event handled!"
|
||||
// }, 500);
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user