Reputation: 2141
I need to have React running a function every number of seconds but the countdown shouldn't be restarted on a re-render of the DOM. So basically I want to run this function when the app starts, and every 30 seconds re-run the same function, but if when the DOM re-render I do not want to have the countdown to 30 seconds restart from 0. I was checking this answer How to call a function every minute in a React component? but each time the DOM renders the countdown is reset to 0. Is there a way to achieve this only using React?
Upvotes: 1
Views: 1166
Reputation: 387
Dennis.
If we could see your code, it'd be easier to provide you with some kind of answer. However, with what I got, I imagine I can help you in a way.
You see, if you want a function to be run every 30s, you can use the setInterval
function for that. And you could store your current timer on your component's state. By doing that, every 1 second, your setInterval
function would update the component's state, re-rendering the component but not reseting the timer, since the timer would retrieve its information directly from the state.
That being said, it would be awesome if we could see the code for what you're trying to do. With the code we can understand if you're working with state, Redux, hooks or anything else.
Hope I could help you clear something out!
Upvotes: 2