Skip to content
Discussion options

You must be logged in to vote

In general when programming: if you want an object exist longer than the function call, you should create it outside of it. In your case the start_task creates a new CountdownTimer object for every time someone accesses the page. Here a short example on how I would approach a global countdown UI:

from datetime import timedelta
from nicegui import app, ui

class Countdown:
    def __init__(self):
        self.remaining = timedelta(seconds=10)
        app.timer(1, self.update)

    def update(self):
        if self.remaining > timedelta(seconds=0):
            self.remaining -= timedelta(seconds=1)
        else:
            self.remaining = timedelta(seconds=0)

    def restart(self):
        

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@rodja
Comment options

rodja Feb 5, 2025
Maintainer

@SHDocter
Comment options

@SHDocter
Comment options

@rodja
Comment options

rodja Feb 6, 2025
Maintainer

Answer selected by SHDocter
@SHDocter
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