Files
fairhopper/hopper/countdown_timer.py
2023-03-30 13:09:23 +02:00

26 lines
635 B
Python

import time
from threading import Event, Thread
from typing import Callable, Optional
class CountdownTimer(Thread):
def __init__(
self, seconds: int, callback: Optional[Callable[[], None]] = None
) -> None:
self.seconds = seconds
self.stop_event = Event()
self.callback = callback
super().__init__()
def run(self) -> None:
cnt = self.seconds
while cnt > 0 and not self.stop_event.is_set():
cnt -= 1
time.sleep(1)
if cnt == 0 and self.callback:
self.callback()
def stop(self) -> None:
self.stop_event.set()