如何取消python中的setInterval?

我正在使用此代码doSomething每 3 秒调用一次函数。但我想要那个时候numeroPeticiones== 3,停止这样做。我找了很多例子,但没有一个有效。就我而言,即使numeroPeticiones== 3满足的条件,我的函数仍将永远执行。我该如何解决?


import threading

from threading import Timer

numeroPeticiones=0


def doSomething():

    global numeroPeticiones

    numeroPeticiones=numeroPeticiones+1

    print ("hello, world",numeroPeticiones)

    if numeroPeticiones == 3:

        print("cancel")

        t.cancel()


def set_interval(func, sec): 

    def func_wrapper():

        set_interval(func, sec) 

        func()  

    t = threading.Timer(sec, func_wrapper)

    t.start()

    return t


肥皂起泡泡
浏览 183回答 1
1回答

明月笑刀无情

您可以执行以下操作,如果 num 等于 3,则不会启动计时器from threading import Timernum = 0def hello():&nbsp; &nbsp; global num&nbsp; &nbsp; num += 1&nbsp; &nbsp; print("hello, world")&nbsp; &nbsp; if (num < 3):&nbsp; &nbsp; &nbsp; &nbsp; Timer(3, hello).start()Timer(3, hello).start()&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python