Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript-like setInterval/clearInterval for Python [duplicate]

Does Python have a function similar to JavaScript's setInterval()?

I would like to have:

def set_interval(func, interval):
    ...

That will call func every interval time units.

like image 414
zjm1126 Avatar asked Jul 09 '26 15:07

zjm1126


1 Answers

This might be the correct snippet you were looking for:

import threading

def set_interval(func, sec):
    def func_wrapper():
        set_interval(func, sec)
        func()
    t = threading.Timer(sec, func_wrapper)
    t.start()
    return t
like image 107
stamat Avatar answered Jul 12 '26 07:07

stamat