Home > Enterprise >  How can I run my function for some number of minutes?
How can I run my function for some number of minutes?

Time:07-03

I am writing a simple program to keep clicking my screen (for roblox clicking simulator). But is there a way to run my function autoclicker for 20 minutes using a while loop? The while true makes the function run infinitely and I only want it to run for 20 minutes.

    import time
    import mouse


    def autoclicker(n):
        time.sleep(5)
        while True:
            for i in range(1, 13):
                mouse.click('left')
            time.sleep(1)

CodePudding user response:

Change the while True to test for the limit:

end = time.time()   20 * 60
while time.time() < end:
    for i in range(1, 13):
        mouse.click('left')
    time.sleep(1)

CodePudding user response:

We can try this

import time
import mouse

def autoclicker(n):

    time_1 = datetime.strptime(datetime.now().strftime("%H:%M:%S"),"%H:%M:%S")  #getting initial time

    minPass = 0
    while minPass < 20:  #check if required time ellapesed 

        time.sleep(5)
        for i in range(1, 13):
            mouse.click('left')
        time.sleep(1)

        time_2 = datetime.strptime(datetime.now().strftime("%H:%M:%S"),"%H:%M:%S") #getting current time
        minPass = int(str(time_2 - time_1).split(':')[1])  #extracting minute part from time difference
  • Related