Python : Basic countdown timer & function() > int()

前端 未结 3 759
温柔的废话
温柔的废话 2021-01-26 03:04

I\'m trying to ucreate a timer function that runs in the background of my code and make it so I can use/check the time. What I mean by use/check, I\'m trying to make it so I can

3条回答
  •  情书的邮戳
    2021-01-26 03:24

    The way you do it, you'll going to have to use multithread.

    Here is another, simpler approach : On your script beginning, set a time_start variable with the number of seconds since the epoch using time.time() Then when you need the number of elapsed seconds, use time.time() - time_start :

    t_start = time.time()
    # do whatever you'd like
    t_current = int(time.time()-t_start) # this way you get the number of seconds elapsed since start.
    

    You can put that in a function as well, defining t_start as a global variable.

    import time
    t_start = time.time()
    
    def timer():
        global t_start
        print(str(int(time.time()-t_start)))
    
    
    print('start')
    time.sleep(2)
    timer()
    time.sleep(3)
    timer()
    

提交回复
热议问题