Spaces:
Paused
Paused
Download utils/data/throttle.ts from matthoffner/chatbot: direct link, hf CLI and curl.
- Browser
- Download file 513 Bytes
-
https://huggingface.co/spaces/matthoffner/chatbot/resolve/main/utils/data/throttle.ts
- Command line
-
hf download hf://spaces/matthoffner/chatbot/utils/data/throttle.ts
-
curl -L -o throttle.ts https://huggingface.co/spaces/matthoffner/chatbot/resolve/main/utils/data/throttle.ts
513 Bytes
| export function throttle<T extends (...args: any[]) => any>( | |
| func: T, | |
| limit: number, | |
| ): T { | |
| let lastFunc: ReturnType<typeof setTimeout>; | |
| let lastRan: number; | |
| return ((...args) => { | |
| if (!lastRan) { | |
| func(...args); | |
| lastRan = Date.now(); | |
| } else { | |
| clearTimeout(lastFunc); | |
| lastFunc = setTimeout(() => { | |
| if (Date.now() - lastRan >= limit) { | |
| func(...args); | |
| lastRan = Date.now(); | |
| } | |
| }, limit - (Date.now() - lastRan)); | |
| } | |
| }) as T; | |
| } | |