Stop clock when page is not on screen [duplicate]

in the title I used a clock as an example but my question is for all functions.
How do I stop some function when it is not on-screen. For the clock I used the useEffect Hook with setInterval(), since I am using ReactJs. So how do I stop the interval when the page is not visible on the screen so it does not slow down the users computer. I read that it is not possible to stop setInterval so I know that I need to clearInterval and and setInterval again.