-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtimer.py
32 lines (27 loc) · 878 Bytes
/
timer.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
import threading
class RTimer(object):
def __init__(self, function):
self._timer = None
self.interval = None
self.function = function
self.is_running = False
self.auto_stop = False
def _run(self):
self.is_running = False
if not self.auto_stop:
self.start()
self.function()
def start(self):
if not self.is_running:
self._timer = threading.Timer(self.interval, self._run)
self._timer.start()
self.is_running = True
def startInterval(self, interval):
self.interval = interval
if not self.is_running:
self._timer = threading.Timer(self.interval, self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False