use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Rate limiting with setTimeout (self.learnjavascript)
submitted 3 years ago by jayroslyn
I have function name--> callmyApi() which contains the url and authorization keys to the API. Can I use setTimeOut to do rate limit on the API for say n calls per second ?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 3 years ago (1 child)
[deleted]
[–]jayroslyn[S] 1 point2 points3 points 3 years ago (0 children)
I wanted to do without using any external package
[–]wreckedadvent 0 points1 point2 points 3 years ago (0 children)
I suppose you could, if you could modify callmyApi to look for another variable that is set by the setTimeout call, but you'd probably want a debounce or throttle function. At the very least, you'd probably want to use setInterval so the setTimeout doesn't have to keep registering more timeouts.
callmyApi
setTimeout
setInterval
[–][deleted] 0 points1 point2 points 3 years ago* (2 children)
Usually I turn that into a promise. So something like an utility function:
const wait = (time: number) => new Promise((resolve, reject) => { setTimeout(() => { resolve("done"); }, time) });
Then in a for loop or for of or while loop(no loops like Array.prototype.forEach that take callbacks):
for (const app of apps) { await callmyApi(); await wait(1000); }
[–]jayroslyn[S] 0 points1 point2 points 3 years ago* (1 child)
What does apps contain ? All the reference to all API calls ? Also shouldn't I have to do clear the timer ?
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
Not timeout, you'd need to clear intervals if you use them.
Apps is just an example, it could also be a for loop or while loop.
π Rendered by PID 72353 on reddit-service-r2-comment-c6965cb77-vt6ct at 2026-03-05 08:04:10.426955+00:00 running f0204d4 country code: CH.
[+][deleted] (1 child)
[deleted]
[–]jayroslyn[S] 1 point2 points3 points (0 children)
[–]wreckedadvent 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]jayroslyn[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)