Skip to content

Instantly share code, notes, and snippets.

@stickperson
Created August 31, 2020 03:25
Show Gist options
  • Save stickperson/5f284ed1b1f9b717617c9ac438343df2 to your computer and use it in GitHub Desktop.
Save stickperson/5f284ed1b1f9b717617c9ac438343df2 to your computer and use it in GitHub Desktop.
basic timer
class Timer:
def __init__(self, delay, fn):
self.delay = delay
self.fn = fn
self.last_updated = datetime.datetime.now()
def tick(self):
now = datetime.datetime.now()
diff = now - self.last_updated
milliseconds = (diff.days * 24 * 60 * 60 + diff.seconds) * 1000 + diff.microseconds / 1000.0
if milliseconds >= self.delay:
self.fn()
self.last_updated = datetime.datetime.now()
if __name__ == '__main__':
def print_hi():
print('hi')
print_timer = Timer(5000, print_hi)
while True:
print_timer.tick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment