我想制作一个 while 循环,在指定时间后不会重复。
我被要求扩展我的答案,因此我将添加注释来解释每一行。而且答案不是 sikuli 特定的,可以用于任何 python/jython 代码。
# import time module
import time
# define how many seconds loop will last
loopForSeconds = 30
# get current time in seconds since epoch
t0 = time.time()
# loop while time delta is less than required (30 seconds in this example)
while time.time()-t0 < loopForSeconds:
# do something in the loop
print "This will repeat for 30 seconds"
# you may want to slow down your loop by adding some sleep time
# in this case add 1 second to slow down the loop
# so there will be about 30 loops for 30 seconds
# depends on other commands in the loop
time.sleep(1)