Most accurate/fastest way to count seconds and display it?

I’m trying to create a timer that records elapsed time as opposed to counting down (similar to the game clock in soccer). I initialize the start time upon app launch, and then would ideally trigger a function call every second that updates the text-component that displays the timer.

Normally I’d use:

import threading
...
threading.Timer(1, update_function)

but I see that the threading module isn’t available on the client side. I can use it on the server side, but I’m worried about speed since server calls take a certain amount of time, and the function itself takes a small amount. I know that it’s pretty much unnoticable but I would like my timer to be as accurate as it can be, so I was wondering what implementation would work best?

Is calling a server function that then executes threading.Timer() fast enough?

datetime is available on the client. I would think that something like this would work - Note this is untested.

from datetime import datetime, timedelta
    
    start_time = datetime.now()
    
    # something happens
    
    elapsed_time = datetime.now() - start_time # This is a timedelta object
    
    elapsed_time_in_secs = elapsed_time.total_seconds()

Thank you for the response! I have my time-obtaining functions tested and they’re pretty much like yours and it works, but I’m still stuck on what the best way to trigger the functions every 1-second, and have the automatic calls be as accurate as possible. Is there another way to automatically trigger a function every n-seconds other than threading.Timer()?

There’s a Timer component you can put on a form that will fire a function every n-seconds.

3 Likes

Gotcha! I don’t know how I missed it in the documentation, thank you so much!

1 Like