Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3839ee7

Browse files
committed
fix
1 parent ce98365 commit 3839ee7

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

‎public/data/javascript.json

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -911,17 +911,31 @@
911911
"author": "axorax"
912912
},
913913
{
914-
"title": "Random string",
915-
"description": "Generates a random string of characters of a certain length",
916-
"code": [
917-
"function makeid(length, characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {",
918-
" return Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * characters.length))).join('');",
919-
"}",
914+
"title": "Rate Limit Function",
915+
"description": "Limits how often a function can be executed within a given time window.",
916+
"code": [
917+
"const rateLimit = (func, limit, timeWindow) => {",
918+
" let queue = [];",
919+
" setInterval(() => {",
920+
" if (queue.length) {",
921+
" const next = queue.shift();",
922+
" func(...next.args);",
923+
" }",
924+
" }, timeWindow);",
925+
" return (...args) => {",
926+
" if (queue.length < limit) {",
927+
" queue.push({ args });",
928+
" }",
929+
" };",
930+
"};",
920931
"",
921-
"console.log(makeid(5), \"1234\" /* (optional) */);"
932+
"// Usage:",
933+
"const fetchData = () => console.log('Fetching data...');",
934+
"const rateLimitedFetch = rateLimit(fetchData, 2, 1000);",
935+
"setInterval(() => rateLimitedFetch(), 200); // Only calls fetchData twice every second"
922936
],
923-
"tags": ["javascript", "function", "random"],
924-
"author": "kruimol"
937+
"tags": ["javascript", "function", "rate-limiting", "utility"],
938+
"author": "axorax"
925939
}
926940
]
927941
},

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /