Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Gustavo Winter
PLUS
Gustavo Winter
Courses Plus Student 27,382 Points

How to limit 1 get request every 10 min?

Hi i'm using Express.

I need to limit the user to make just 1 Get request to the API at each 10 minutes.

I really have no idea on how to do it, i'll appreciate any help

thanks.

2 Answers

If you want to limit your user to only allow hitting the API end point every 10 minutes I'd store a lastRun field with the user in your database. When your user launches the app send the lastRun time stamp with it. When the user then makes a request verify if 10 minutes have passed by comparing lastRun with the actual time. If 10 minutes did pass, send the API request. In the response then include the new lastRun time

Steven Parker
Steven Parker
231,007 Points

This is actually a better idea if your purpose is to impose a limit instead of an automated refresh. :+1:

Steven Parker
Steven Parker
231,007 Points

The "setInterval" function might be good for this. For example, if you had a function named "makeApiRequest" and wanted to run it every 10 minutes:

var timer = setInterval(makeApiRequest, 600000);  // 600K milliseconds = 10 minutes

Note that the first request won't occur until 10 minutes later. Do a separate one if you want one to happen right away.

If you later want to stop the cycle you can call "clearInterval(timer);".