-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.py
More file actions
42 lines (36 loc) · 876 Bytes
/
Copy pathTimer.py
File metadata and controls
42 lines (36 loc) · 876 Bytes
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
37
38
39
40
41
42
import uuid
class Timer(object):
def __init__(self):
self.timers = []
def add(self, interval, f, repeat = -1):
options = {
"interval" : interval,
"callback" : f,
"repeat" : repeat,
"times" : 0,
"time" : 0,
"uuid" : uuid.uuid4()
}
self.timers.append(options)
return options["uuid"]
def destroy(self, uuid_nr):
for timer in self.timers:
if timer["uuid"] == uuid_nr:
self.timers.remove(timer)
return
def update(self, time_passed):
for timer in self.timers:
timer["time"] += time_passed
if timer["time"] > timer["interval"]:
timer["time"] -= timer["interval"]
timer["times"] += 1
if timer["repeat"] > -1 and timer["times"] == timer["repeat"]:
self.timers.remove(timer)
try:
timer["callback"]()
except:
try:
self.timers.remove(timer)
except:
pass
gtimer = Timer()