forked from 77media/video-flow
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
import { ScriptSlice, ScriptSliceType } from "@/app/service/domain/valueObject";
|
||
|
||
export function parseScriptEntity(text: string):ScriptSlice {
|
||
const scriptSlice = new ScriptSlice(
|
||
// 生成唯一ID,单次使用即可
|
||
`${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`,
|
||
ScriptSliceType.text,
|
||
text,
|
||
{}
|
||
);
|
||
return scriptSlice;
|
||
}
|
||
|
||
|
||
/**
|
||
* @description 节流函数,限制函数在指定时间间隔内只执行一次
|
||
* @param {Function} func - 需要被节流的函数
|
||
* @param {number} delay - 节流时间间隔(毫秒)
|
||
* @returns {Function} - 节流后的新函数
|
||
* @throws {Error} - 如果参数类型不正确
|
||
* @example
|
||
* const throttledFn = throttle(() => { console.log('触发'); }, 1000);
|
||
* window.addEventListener('resize', throttledFn);
|
||
*/
|
||
export function throttle<T extends (...args: any[]) => any>(func: T, delay: number=100): (...args: Parameters<T>) => void {
|
||
if (typeof delay !== 'number' || delay < 0) {
|
||
throw new Error('throttle: 第二个参数必须是非负数');
|
||
}
|
||
let lastCall = 0;
|
||
return (...args: Parameters<T>) => {
|
||
const now = Date.now();
|
||
if (now - lastCall >= delay) {
|
||
lastCall = now;
|
||
func(...args);
|
||
}
|
||
};
|
||
}
|