Skip to content

Page unload event doesn't stop timers #4241

Answered by falkoschindler
flintcreek asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for the warm words, @flintcreek! 😊

Where would I set the handler for the unload event?

It looks like we can add some custom JavaScript to register the window's "beforeunload" event and forward it to the server:

@ui.page('/')
def page():
    ui.add_head_html('''
        <script>
        window.addEventListener('beforeunload', (e) => emitEvent('close'));
        </script>
    ''')
    t = ui.timer(0.5, lambda: print(time.time()))
    ui.on('close', t.cancel)

is there a way to explicitly ask the server if the connection is valid?

It certainly depends on you definition of "valid". But I'm not aware of a way to get the immediate information that a client has left the page.

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@falkoschindler
Comment options

Answer selected by flintcreek
@flintcreek
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants