按你的逻辑,改了一下
def timer(*dargs, **dkargs):
def wrapper(func):
def _wrapper(*args, **kwargs):
if 'step' in dkargs and 'duration' in dkargs:
duration = dkargs['duration']
step = dkargs['step']
last_time = time.time()
# print(step,duration)
while duration >= 0:
current_time = time.time()
# print(step, duration,current_time - last_time)
if current_time - last_time > step:
func(*args, **kwargs)
last_time = current_time
duration -= 1
else:
pass
return func(*args, **kwargs)
return _wrapper
return wrapper
#100 秒太久了, 我改成1秒跑一下,好看效果
@timer(step= 1, duration = 10)
def func(x):
x -= 1
print(x)
return x