0

I am trying to code a function that recognizes when X amount of seconds has passed without pausing the program, so without using time.sleep. How would I be able to do that by using the unix timestamp? So for the code below it would print 5 seconds has passed for every 5 seconds that passes.

import time
X = 5
TIME = time.time()
tony selcuk
  • 709
  • 3
  • 11
  • You should use threads in order to continue the program without stopping the main thread. – scmanjarrez Jun 24 '21 at 16:45
  • 1
    Does this answer your question? [Python threading.timer - repeat function every 'n' seconds](https://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds) – scmanjarrez Jun 24 '21 at 16:45

1 Answers1

0

Check this will help you avoid using time.sleep() and your code will be iterating till the time reaches next X seconds

import time
startTime = time.time()
X = 5
timeDif = 0
while timeDif < X:
    currentTime = time.time()
    timeDif = currentTime - startTime
THUNDER 07
  • 521
  • 5
  • 21