You don't have control on leaving the page. The user can leave on several ways, even a rboot of his system.
You can add an timer on the page that triggers a microflow that writes a dattime attribute, to say the user is still active.
Read this attribute to dicide the user still has his browser open.
Like Martin already said, a user can leave the application on multiple ways where you dont have control of. What you can do, is chagne the session timeout. See https://docs.mendix.com/refguide/custom-settings for more information.