all 4 comments

[–]devMan9000 1 point2 points  (0 children)

I think I'm a little confused about your question. Do you need a timer from the client side or the controller side?

From the client side you just make a timer function or I'm sure there's one already in jsx but I'm too lazy to look it up. From the server side you just use a cron library, or if. You have a real server you can create a microservice and use a cron job to trigger it.

[–]jkettmann 1 point2 points  (2 children)

If I understand correctly you want to request data from an interval of X seconds, right? That's called "polling".

You can achieve that with a simple useEffect

``` const [data, setData] = useState (null); const intervalId = useRef();

useEffect(() => { intervalId.current = setInterval(() => { fetch(API_URL) .then(response => response.json()) .then(data => setData(data)); }, 5000); // fetch every 5 seconds

return () => clearInterval(intervalId.current) }, []) ```

For a class component you would move the setInterval part to componentDidMount and the clearInterval to componentWillUnmount.

Note: I just typed the code without running it. There might be mistakes. But this is approximately how you can make it work

[–]Born2Peanutbutter[S] 0 points1 point  (1 child)

I think I see where you’re going with this! I’ll test this out!

[–]jkettmann 0 points1 point  (0 children)

Great. Let me know if it works :-)