How to detect page reload / closed dialog after reload

I open a dialog and start a timer which periodically updates a value in this dialog. The timer is cleaned up if the dialog is closed. If I reload the page the timer is still running and I don’t know how to detect that the dialog is not displayed anymore. This also applies if the browser tab is closed.

Thanks for help!
Best regards.