← Go Back

How to Run Code After a Certain Time

Using the standard threading.Timer class:

import threading

def f():
print("Hello, world!")

# Execute the function after 3 seconds.
t = threading.Timer(3, f)
print("This gets executed before the f() function.")

The first argument of Timer indicates the number of seconds within which the function passed as the second argument will be executed.

threading timer