Created
August 31, 2020 03:25
-
-
Save stickperson/5f284ed1b1f9b717617c9ac438343df2 to your computer and use it in GitHub Desktop.
basic timer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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