Skip to content
Advertisement

React execute a function at varying time intervals [closed]

I have an API which returns a list of objects. Each of these objects have a field called timeOut which contains some time interval in secs. Once the time interval of any object expires, some operation needs to performed on that particular object.

How can I achieve this in react ? I considered using setInterval on each object in the list, but it looks like a bad design.

Any help will be appreciated. Thank you.

Advertisement

Answer

You should use setTimeout instead of setInterval if you want an operation to run only once after the given time period. I believe this is your best bet to accomplish this task.

Otherwise, you will have to dive into even crazier designs like comparing the present date-time object from new Date() with the one you generated right after fetching your data, which will not only be complicated but also, more error-prone and inaccurate.

Let me know if you want help with the first method.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement