Skip to content

Instantly share code, notes, and snippets.

@catwhocode
Created February 18, 2025 04:26
Show Gist options
  • Save catwhocode/0fbd3fe2ade2dcb05156d4665e5f7db7 to your computer and use it in GitHub Desktop.
Save catwhocode/0fbd3fe2ade2dcb05156d4665e5f7db7 to your computer and use it in GitHub Desktop.
Debounce Decorator
import functools
from threading import Timer
# source:
# https://stackoverflow.com/questions/61476962/python-decorator-for-debouncing-including-function-arguments
def debounce(timeout: float):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
wrapper.func.cancel()
wrapper.func = Timer(timeout, func, args, kwargs)
wrapper.func.start()
wrapper.func = Timer(timeout, lambda: None)
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment