27 lines
770 B
Python
27 lines
770 B
Python
import threading
|
|
|
|
|
|
def debounce(wait_time):
|
|
"""
|
|
Decorator that will debounce a function so that it is called after wait_time seconds
|
|
If it is called multiple times, will wait for the last call to be debounced and run only this one.
|
|
See the test_debounce.py file for examples
|
|
"""
|
|
|
|
def decorator(function):
|
|
def debounced(*args, **kwargs):
|
|
def call_function():
|
|
debounced._timer = None
|
|
return function(*args, **kwargs)
|
|
|
|
if debounced._timer is not None:
|
|
debounced._timer.cancel()
|
|
|
|
debounced._timer = threading.Timer(wait_time, call_function)
|
|
debounced._timer.start()
|
|
|
|
debounced._timer = None
|
|
return debounced
|
|
|
|
return decorator
|