Re: Best way to make a timer in python?
Hi kingzombie,
From reading your question, I'm not sure if you want a timer to just wait a set amount of time while nothing else happens or if you want to perform some event a set amount of time after something else happens.
If you don't need to do anything and just want to wait for a period of time to elapse then you can just call sleep(3) etc to sleep (i.e. wait) for three seconds in this example. This is what's called a blocking operation because the code doesn't continue until it has waited for the specified duration.
If you want other things to happen while waiting for a period of time to elapse then you need a non-blocking way of doing this. Here's a quick example of a rubbish programming joke I knocked together for you:
import threading
from time import sleep
def answer():
print("Java.")
print("Knock knock?") print("Who's there?") timer = threading.Timer(10.0, answer) timer.start() # After 10 seconds, the 'answer' function will be called ## (in the meanwhile you can do other stuff...) for loop in range(1, 11): print('.') # Print a full stop sleep(1) # Sleep for 1 second
This produces the output:
Knock knock?
Who's there?
.
.
.
.
.
.
.
.
.
.
Java.
The knock-knock / who's there appears immediately, then we wait 10 seconds, printing one full-stop per second, then finally the 10 second timer expires and the 'answer' function is called to print "Java".
-- Audiogames-reflector mailing list Audiogames-reflector@sabahattin-gucukoglu.com https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector