-
Notifications
You must be signed in to change notification settings - Fork 576
/
utils.py
37 lines (29 loc) · 1.1 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Timer(object):
""" A Timer that can periodically call a given callback
function.
After creation, you should call update() with the
amount of time passed since the last call to update()
in milliseconds.
The callback calls will result synchronously during these
calls to update()
"""
def __init__(self, interval, callback, oneshot=False):
""" Create a new Timer.
interval: The timer interval in milliseconds
callback: Callable, to call when each interval expires
oneshot: True for a timer that only acts once
"""
self.interval = interval
self.callback = callback
self.oneshot = oneshot
self.time = 0
self.alive = True
def update(self, time_passed):
if not self.alive:
return
self.time += time_passed
if self.time > self.interval:
self.time -= self.interval
self.callback()
if self.oneshot:
self.alive = False