Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139805

React keep interval running in the background

$
0
0

I can't find an answer to my question anywhere.

long story short i have a web app (MERN stack) and i want to keep making requests from the frontend (react) to the backend (node) periodically EVEN WHEN THE COMPONENT UNMOUNTS or the user closes the page.

Currently this is my code and its working fine if I am in that particular page/route. but it resets the interval if i close the page and come back.

let updateCycle; // to make sure there is only 1 interval
useEffect( () => {
if (!updateCycle)
 updateCycle = setInterval(() => {
     myFunction() // calling the function
     }, 10 * 60 * 1000); // every 10 mins
},[myFunction])

So how can i do that ? or do i have to move the whole thing to the backend ?


Viewing all articles
Browse latest Browse all 139805

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>