Search code examples
pythontimetimer

Timer update inside while loop & if statement?


I'm building my first timer in Python and looking into time module. This is what I want:

  1. Inside while loop, when the if condition "A" is true for the first time, start timer

  2. When 10 seconds has passed, trigger an event

  3. When 30 seconds has passed, clear timer so that it's ready to start again when if condition "A" is true

I feel like I have all the building blocks but still cannot get my head around for starting the 10 second timer. In pseudocode, I want this:

if current_time - the_time_from_first_if_statement_match > 10:
        do something

This is what I have now. Any help appreciated!

def main():

start_time = time.time() 
time_limit = 10

while True:
   current_time = time.time() #keeps updating
   matchresult = video_process.match

   if matchresult == True:
      elapsed_time = current_time - start_time #can be anything like 14 or 100 at this point, I'd just want a 10 sec timer

      if elapsed_time > time_limit:
         print("do something")


Solution

  • Something along these lines:

    while True:
        while not condition:
            ...
        start_time = ...
        while elapsed_time < 10:
            ...
        do_something
        while elapsed_time < 30:
            ...