0

I have a simple python class called Timer_ that takes an interval in seconds and runs the timer in the background. When it is done, it runs a finished function that returns true. How would I get the value of this function when the timer stops in the form of a variable? Thank you!

import threading



class Timer_():

    def __init__(self, interval):
        #interval in seconds
        self.interval = interval
        self.finished = False

    def run(self):
        self.timel = threading.Timer(float(self.interval), self.finish)
        self.timel.start()

    def finish(self):
        self.finished = True
        return True

    def cancel(self):
        self.timel.cancel()



time = Timer_(5)
time.run()
print(time.finished)
divibisan
  • 11,659
  • 11
  • 40
  • 58
Nick D
  • 590
  • 3
  • 10
  • 21

1 Answers1

0

Sounds like you want to wait until time.finished is True

Try using a while loop over the value of time.finished:

time = Timer_(5)
timer.run()
while not timer.finished:
    pass
x = timer.finished
print(x)

This will loop through the while until timer.finished is True then you can use that value as a variable or continue on.

degenTy
  • 340
  • 1
  • 9