| | |
| | | // 工具类函数 |
| | | |
| | | import { SpeedTime } from '@/widgets/ProductDelivery/enum' |
| | | |
| | | // 随机id |
| | | function createRandomId(): string { |
| | | return ( |
| | |
| | | var uniqueValues = new Set(values) |
| | | return values.length !== uniqueValues.size |
| | | } |
| | | /** |
| | | * 首字母小写 |
| | | * @param str |
| | | * @returns |
| | | */ |
| | | export function lowerCaseFirstChar(str: string) { |
| | | if (!str) return str // 如果字符串为空,则直接返回 |
| | | return str.charAt(0).toLowerCase() + str.slice(1) |
| | | } |
| | | |
| | | // export const genSetInterval = (fn: () => void, time = 33) => { |
| | | // let timer: any = null |
| | | // return { |
| | | // start() { |
| | | // timer && clearInterval(timer) |
| | | // timer = setInterval(() => fn(), time) |
| | | // }, |
| | | // clear() { |
| | | // if (timer) { |
| | | // clearInterval(timer) |
| | | // timer = null |
| | | // } |
| | | // }, |
| | | // } |
| | | // } |
| | | type Timer = { |
| | | start: () => void |
| | | clear: () => void |
| | | } |
| | | |
| | | export const genSetInterval = (fn: () => void, time: number = 33): Timer => { |
| | | let timerId: number | null = null |
| | | let lastTime = performance.now() |
| | | |
| | | const loop = (currentTime: number) => { |
| | | const deltaTime = currentTime - lastTime |
| | | |
| | | if (deltaTime >= time) { |
| | | fn() |
| | | lastTime = currentTime - (deltaTime % time) // 保留余下的时间,减少漂移 |
| | | } |
| | | |
| | | timerId = requestAnimationFrame(loop) |
| | | } |
| | | |
| | | return { |
| | | start() { |
| | | if (timerId === null) { |
| | | lastTime = performance.now() // 重置初始时间 |
| | | timerId = requestAnimationFrame(loop) |
| | | } |
| | | }, |
| | | clear() { |
| | | if (timerId !== null) { |
| | | cancelAnimationFrame(timerId) |
| | | timerId = null |
| | | } |
| | | }, |
| | | } |
| | | } |
| | | |
| | | export { |
| | | hasDuplicates, |